home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1999 September (IDG) / Sep99.iso / Shareware World / Info / For Developers / PlayerPRO 5.2 Dev.Kit Mac / MADH Library 5.2 / MADLibrary Source / MainDriver.c < prev    next >
Encoding:
Text File  |  1999-06-23  |  71.8 KB  |  3,249 lines  |  [TEXT/CWIE]

  1. /********************                        ***********************/
  2. //
  3. //    Player PRO 5.0 - DRIVER SOURCE CODE -
  4. //
  5. //    Library Version 5.0
  6. //
  7. //    To use with MAD Library for Mac: Symantec, CodeWarrior and MPW
  8. //
  9. //    Antoine ROSSET
  10. //    16 Tranchees
  11. //    1206 GENEVA
  12. //    SWITZERLAND
  13. //
  14. //    COPYRIGHT ANTOINE ROSSET 1996, 1997, 1998
  15. //
  16. //    Thank you for your interest in PlayerPRO !
  17. //
  18. //    FAX:                (+41 22) 346 11 97
  19. //    PHONE:             (+41 79) 203 74 62
  20. //    Internet:     RossetAntoine@bluewin.ch
  21. //
  22. /********************                        ***********************/
  23.  
  24. #include "RDriver.h"
  25. #include "RDriverInt.h"
  26. #include "FileUtils.h"
  27. #include "string.h"
  28.  
  29. #ifdef _MIDIHARDWARE_
  30. #include "OMS.h"
  31. #endif
  32.  
  33. #ifdef _MAC_H
  34. #include <Sound.h>
  35. #include <SoundInput.h>
  36. #include <SoundComponents.h>
  37. #endif
  38.  
  39. ///////////////////////////////
  40.  
  41. #define  NO_ASM
  42.  
  43. #define rate5khz    0x15BB9B5CUL
  44. #define rate16khz    0x4132DDF2UL
  45. #define rate48khz    0xBB800000UL
  46.  
  47. enum {
  48.     MADFileType = 1,
  49.     MADRsrcType = 2,
  50.     MADPtrType = 3
  51. };
  52.  
  53.  
  54. void SendMIDIClock( MADDriverRec *intDriver, Byte MIDIByte);
  55.  
  56. /*MADMusic* CreateFreeMADI()
  57. {
  58.     MADMusic    *music = (MADMusic*) NewPtrClear( sizeof( MADMusic));
  59.     MADSpec        *MADI;
  60.     short        i, x, z;
  61.     FSSpec        aSpec;
  62.     Cmd            *aCmd;
  63.     
  64.     // ******* HEADER *********
  65.     
  66.     MADI = (MADSpec*) MADNewPtrClear( sizeof( MADSpec), intDriver->lib);
  67.     
  68.     MADI->MAD                    = MADID;
  69.     MADI->numPat                = 1;
  70.     MADI->numPointers            = 1;
  71.     MADI->numInstru                = 0;
  72.     MADI->numChn                = 4;
  73.     MADI->speed                    = 6;
  74.     MADI->tempo                    = 125;
  75.     
  76.     music->header    = MADI;
  77.     
  78.     for( i = 0; i < MAXTRACK; i++)
  79.     {
  80.         if( i % 2 == 0) MADI->chanPan[ i] = MAX_PANNING/4;
  81.         else MADI->chanPan[ i] = MAX_PANNING - MAX_PANNING/4;
  82.         
  83.         MADI->chanVol[ i] = MAX_VOLUME;
  84.     }
  85.     
  86.     music->fid = (InstrData*) MADNewPtrClear( sizeof( InstrData) * (long) MAXINSTRU, intDriver->lib);
  87.     
  88.     music->sample = (sData**) MADNewPtrClear( sizeof( sData*) * (long) MAXINSTRU * (long) MAXSAMPLE, intDriver->lib);
  89.     
  90.     // ******* PARTITION *********
  91.     
  92.     for(i=0; i< MADI->numPat; i++)
  93.     {
  94.         music->partition[ i] = (PatData*) MADNewPtrClear( sizeof( PatHeader) + MADI->numChn * 64L * sizeof( Cmd), intDriver->lib);
  95.         
  96.         music->partition[ i]->header.size         = 64L;
  97.         music->partition[ i]->header.compMode     = 'NONE';
  98.         music->partition[ i]->header.patBytes     = 0L;
  99.         music->partition[ i]->header.unused2         = 0L;
  100.         
  101.         for( x = 0; x < 64; x++)
  102.         {
  103.             for( z = 0; z < music->header->numChn; z++)
  104.             {
  105.                 aCmd = GetMADCommand(  x,  z, music->partition[ i]);
  106.                 
  107.                 MADKillCmd( aCmd);
  108.             }
  109.         }
  110.     }
  111.     for( i = music->header->numPat; i < MAXPATTERN ; i++) music->partition[ i] = 0L;
  112.     
  113.     return music;
  114. }
  115.  
  116. void WriteMADHFile( FSSpec *newFile)
  117. {
  118.     short    fRefNum;
  119.     OSErr    iErr;
  120.     long    inOutCount;
  121.  
  122.     iErr = FSOpen( newFile->name, 0, &fRefNum);
  123.     
  124.     if( iErr != noErr)
  125.     {
  126.         iErr = Create( newFile->name, 0, 'SNPL', theType);
  127.         iErr = FSOpen( newFile->name, 0, &fRefNum);
  128.     }
  129.  
  130.     inOutCount = sizeof( MADSpec);
  131.     iErr = FSWrite( fRefNum, &inOutCount, MDriver->header);
  132.     
  133.     for( i = 0; i < MDriver->header->numPat ; i++)
  134.     {
  135.         if( thePrefs.MADCompression) MDriver->partition[ i]->header.compMode = 'MAD1';
  136.         
  137.         if( MDriver->partition[ i]->header.compMode == 'MAD1')
  138.         {
  139.             PatMAD = CompressPartitionMAD1( MDriver->partition[ i]);
  140.             inOutCount = PatMAD->header.PatBytes + sizeof( PatHeader);
  141.             
  142.             iErr = FSWrite( fRefNum, &inOutCount, PatMAD);
  143.             
  144.             MyDisposePtr( (Ptr*) &PatMAD);
  145.         }
  146.         else
  147.         {
  148.             inOutCount = sizeof( PatHeader);
  149.             inOutCount += MDriver->header->numChn * MDriver->partition[ i]->header.size * sizeof( Cmd);
  150.  
  151.             iErr = FSWrite( fRefNum, &inOutCount, MDriver->partition[ i]);
  152.         }
  153.     }
  154.     
  155.     for( i = 0; i < MDriver->header->numInstru ; i++)
  156.     {
  157.         inOutCount = MDriver->header->fid[i].size;
  158.         if( inOutCount > 0)
  159.         {
  160.             iErr = FSWrite( fRefNum, &inOutCount, MDriver->instrument[i]);
  161.         }
  162.     }
  163.     GetFPos( fRefNum, &tt);
  164.     SetEOF( fRefNum, tt);
  165.     FSClose( fRefNum);
  166. }
  167. */
  168.  
  169. long MADGetMusicSize( MADMusic    *music)
  170. {
  171.     short i, x;
  172.     long fileSize;
  173.     
  174.     if( music->header == 0L) return 0L;
  175.     
  176.     fileSize = sizeof( MADSpec);
  177.     fileSize += (long) music->header->numInstru * sizeof( InstrData);
  178.     for( i = 0; i < music->header->numPat; i++) fileSize += sizeof( PatHeader) + music->header->numChn * music->partition[ i]->header.size * sizeof( Cmd);
  179.     for( i = 0; i < music->header->numInstru ; i++)
  180.     {
  181.         for( x = 0; x < music->fid[ i].numSamples ; x++)
  182.         {
  183.             sData    *curData = music->sample[ i * MAXSAMPLE + x];
  184.             
  185.             fileSize += sizeof( sData);
  186.             fileSize += curData->size;
  187.         }
  188.     }
  189.     
  190.     return fileSize;
  191. }
  192.  
  193. #ifdef _MAC_H
  194. void ConvertTo64Rows( MADMusic *music)
  195. {
  196.     long        i, x, z;
  197.     Boolean        IsReading;
  198.     
  199.     if( music->header == 0L) return;
  200.     
  201.     IsReading = music->musicUnderModification;
  202.     music->musicUnderModification = true;
  203.     
  204.     for( i = 0; i < music->header->numPat; i++)
  205.     {
  206.         // Resize pattern to 64 rows and put a pattern break
  207.         
  208.         long        newSize;
  209.         PatData        *newPat;
  210.         
  211.         newSize = sizeof( PatHeader) + music->header->numChn * 64L * sizeof( Cmd);
  212.         
  213.         if( music->partition[ i]->header.size < 64)
  214.         {
  215.             Cmd        *srccmd, *dstcmd;
  216.             long    patsize;
  217.             
  218.             newPat = ( PatData*) NewPtrClear( newSize);
  219.             
  220.             newPat->header.size         = 64L;
  221.             newPat->header.compMode     = 'NONE';
  222.             newPat->header.patBytes     = 0L;
  223.             newPat->header.unused2         = 0;
  224.             
  225.             BlockMoveData( music->partition[ i]->header.name, newPat->header.name, 32);
  226.             
  227.             // Upgrade length to 64
  228.             
  229.             for( x = 0, patsize = 0; x < 64; x++, patsize++)
  230.             {
  231.                 for( z = 0; z < music->header->numChn; z++)
  232.                 {
  233.                     if( x < music->partition[ i]->header.size)
  234.                     {
  235.                         // src
  236.                         srccmd = GetMADCommand( patsize, z, music->partition[ i]);
  237.                         
  238.                         // dst
  239.                         dstcmd = GetMADCommand( x, z, newPat);
  240.                         
  241.                         *dstcmd = *srccmd;
  242.                     }
  243.                     else
  244.                     {
  245.                         dstcmd = GetMADCommand( x, z, newPat);
  246.                         
  247.                         MADKillCmd( dstcmd);
  248.                     }
  249.                 }
  250.             }
  251.             
  252.             // dst
  253.             dstcmd = GetMADCommand( music->partition[ i]->header.size-1, 0, newPat);        // Pose le pattern break
  254.             
  255.             MADKillCmd( dstcmd);
  256.             
  257.             dstcmd->cmd = skipE;
  258.             dstcmd->arg = 0;
  259.             
  260.             // Replace old pattern
  261.             
  262.             DisposePtr( ( Ptr) music->partition[ i]);
  263.             music->partition[ i] = 0L;
  264.             
  265.             music->partition[ i] = newPat;
  266.         }
  267.         else if( music->partition[ i]->header.size > 64)
  268.         {
  269.             long        patID = i;
  270.             long         patsize = 0;
  271.             PatData*    srcPat = music->partition[ i];
  272.             
  273.             while( patsize < srcPat->header.size)
  274.             {
  275.                 newPat = ( PatData*) NewPtrClear( newSize);
  276.                 
  277.                 newPat->header.size         = 64L;
  278.                 newPat->header.compMode     = 'NONE';
  279.                 newPat->header.patBytes     = 0L;
  280.                 newPat->header.unused2         = 0;
  281.                 
  282.                 BlockMoveData( srcPat->header.name, newPat->header.name, 32);
  283.                 
  284.                 for( x = 0; x < 64; x++, patsize++)
  285.                 {
  286.                     for( z = 0; z < music->header->numChn; z++)
  287.                     {
  288.                         Cmd        *srccmd, *dstcmd;
  289.                         
  290.                         if( patsize < srcPat->header.size)
  291.                         {
  292.                             // src
  293.                             srccmd = GetMADCommand( patsize, z, srcPat);
  294.                             
  295.                             // dst
  296.                             dstcmd = GetMADCommand( x, z, newPat);
  297.                             
  298.                             *dstcmd = *srccmd;
  299.                         }
  300.                         else
  301.                         {
  302.                             dstcmd = GetMADCommand( x, z, newPat);
  303.                             MADKillCmd( dstcmd);
  304.                         }
  305.                     }
  306.                 }
  307.                 
  308.                 // Update patterns list & partition
  309.                 
  310.                 if( patID != i)
  311.                 {
  312.                     for( x = music->header->numPat; x > patID ; x--)
  313.                     {
  314.                         music->partition[ x] = music->partition[ x - 1];
  315.                     }
  316.                     music->header->numPat++;
  317.                 }
  318.                 
  319.                 for( x = music->header->numPat; x < MAXPATTERN; x++) music->partition[ x] = 0L;
  320.                 
  321.                 music->partition[ patID] = newPat;
  322.                 
  323.                 patID++;
  324.             }
  325.             
  326.             // Do we need a pattern break ?
  327.             
  328.             if( (srcPat->header.size / 64) * 64 != srcPat->header.size)
  329.             {
  330.                 short    breakpos;
  331.                 Cmd        *dstcmd;
  332.                 
  333.                 breakpos = srcPat->header.size - (srcPat->header.size / 64) * 64;
  334.                 
  335.                 dstcmd = GetMADCommand( breakpos-1, 0, newPat);
  336.                 MADKillCmd( dstcmd);
  337.                 
  338.                 dstcmd->cmd = skipE;
  339.                 dstcmd->arg = 0;
  340.             }
  341.             
  342.             // Update la partition list
  343.             
  344.             for( x = 0; x < music->header->numPointers; x++)
  345.             {
  346.                 if( music->header->oPointers[ x] > patID) music->header->oPointers[ x] += patID - i;
  347.                 else if( music->header->oPointers[ x] == i)
  348.                 {
  349.                     // Avance le reste de la partition
  350.                     
  351.                     for( z = x; z < 256; z++)
  352.                     {
  353.                         if( x + patID - i - 1 < 256)
  354.                         {
  355.                             music->header->oPointers[ x + patID - i - 1] = music->header->oPointers[ x];
  356.                         }
  357.                     }
  358.                     
  359.                     for( z = i; z < patID; z++)
  360.                     {
  361.                         music->header->oPointers[ x] = z;
  362.                         
  363.                         x++;
  364.                     }
  365.                     x--;
  366.                     
  367.                     music->header->numPointers += patID - i - 1;
  368.                 }
  369.             }
  370.             
  371.             DisposePtr( ( Ptr) srcPat);
  372.         }
  373.     }
  374.     
  375.     music->musicUnderModification = IsReading;
  376. }
  377.  
  378. long MADMinimize( MADMusic *music)
  379. {
  380.     short         i, x, z;
  381.     Boolean        remove, IsReading;
  382.     Boolean        inst[ MAXINSTRU];
  383.      long        before, after;
  384.      
  385.      if( music->header == 0L) return 0L;
  386.      
  387.     IsReading = music->musicUnderModification;
  388.     music->musicUnderModification = true;
  389.      
  390.     // Check size before
  391.     
  392.     before = MADGetMusicSize( music);
  393.     
  394.     // Check for unused patterns
  395.     
  396.     if( music->header->numPat > 1)
  397.     {
  398.         for( i = 0; i < music->header->numPat; i++)
  399.         {
  400.             remove = true;
  401.             for( x = 0; x < music->header->numPointers; x++)
  402.             {
  403.                 if( music->header->oPointers[ x] == i)
  404.                 {
  405.                     remove = false;
  406.                     break;
  407.                 }
  408.             }
  409.             
  410.             if( remove)
  411.             {
  412.                 music->header->numPat--;
  413.                 
  414.                 DisposePtr( ( Ptr) music->partition[ i]);
  415.                 music->partition[ i] = 0L;
  416.                 
  417.                 for( x = i; x < music->header->numPat; x++)
  418.                 {
  419.                     music->partition[ x] = music->partition[ x + 1];
  420.                 }
  421.                 for( x = music->header->numPat; x < MAXPATTERN; x++) music->partition[ x] = 0L;
  422.                 
  423.                 /***** Update la partition list ******/
  424.                 
  425.                 for( x = 0; x < 128; x++)
  426.                 {
  427.                     if( music->header->oPointers[ x] > i) music->header->oPointers[ x]--;
  428.                     else if( music->header->oPointers[ x] == i) music->header->oPointers[ x] = 0;
  429.                 }
  430.                 
  431.                 if( i > 0) i--;
  432.             }
  433.         }
  434.     }
  435.     
  436.     // Check for unused instruments
  437.     for( i = 0; i < 32 ; i++) inst[ i] = false;
  438.     
  439.     for( i = 0; i < music->header->numPat; i++)
  440.     {
  441.         for( x = 0; x < music->partition[ i]->header.size; x++)
  442.         {
  443.             for( z = 0; z < music->header->numChn; z++)
  444.             {
  445.                 Cmd        *cmd;
  446.                 
  447.                 cmd = GetMADCommand( x, z, music->partition[ i]);
  448.                 
  449.                 if( cmd->ins > 0) inst[ cmd->ins-1] = true;
  450.             }
  451.         }
  452.     }
  453.     
  454.     for( i = 0; i < MAXINSTRU; i++)
  455.     {
  456.         if( inst[ i] == false) MADKillInstrument( music, i);
  457.     }
  458.     
  459.     // Check for after loop data
  460.     for( i = 0; i < MAXINSTRU; i++)
  461.     {
  462.         for( x = 0; x < music->fid[ i].numSamples; x++)
  463.         {
  464.             sData    *curData = music->sample[ i * MAXSAMPLE + x];
  465.             
  466.             if( curData->loopSize > 2)
  467.             {
  468.                 if( curData->size > curData->loopBeg + curData->loopSize)
  469.                 {
  470.                     SetPtrSize( curData->data, curData->loopBeg + curData->loopSize);
  471.                     
  472.                     curData->size = curData->loopBeg + curData->loopSize;
  473.                 }
  474.             }
  475.         }
  476.     }
  477.     
  478.     music->musicUnderModification = IsReading;
  479.     
  480.     after = MADGetMusicSize( music);
  481.     
  482.     return before - after;
  483. }
  484. #endif
  485.  
  486. Ptr MADNewPtr( long size, MADLibrary* init)
  487. {
  488.     if( size == 0) size = 1;    // Myriad, zeroptr on PC
  489.     
  490.     if( init->sysMemory) return NewPtrSys( size);
  491.     else return NewPtr( size);
  492. }
  493.  
  494. Ptr MADNewPtrClear( long size, MADLibrary* init)
  495. {
  496.     if( init->sysMemory) return NewPtrSysClear( size);
  497.     else return NewPtrClear( size);
  498. }
  499.  
  500. void MADGetBestDriver( MADDriverSettings    *Init)
  501. {
  502. #ifdef _MAC_H
  503. long                gestaltAnswer;
  504. short                myBit;
  505. NumVersion    nVers;
  506. Boolean            Stereo, StereoMixing, NewSoundManager, NewSoundManager31, hasASC, Audio16;
  507.  
  508. /***************                    ****************/
  509. /****** HARDWARE IDENTIFICATION AND CHECK **********/
  510. /***************                    ****************/
  511.  
  512. /****************/
  513. /** ASC CHIP ? **/
  514. /****************/
  515.  
  516.     Gestalt( kSoundOutputDeviceType, &gestaltAnswer);
  517.     if( gestaltAnswer == kASCSubType) hasASC = true;
  518.     else hasASC = false;
  519.  
  520. /**************/
  521. /** STEREO ? **/
  522. /**************/
  523.  
  524.     Gestalt( gestaltSoundAttr, &gestaltAnswer);
  525.     myBit = gestaltStereoCapability;
  526.     Stereo = BitTst( &gestaltAnswer, 31-myBit);
  527.  
  528. /******************************/
  529. /** INTERNAL STEREO MIXING ? **/
  530. /******************************/
  531.  
  532.     myBit = gestaltStereoMixing;
  533.     StereoMixing = BitTst( &gestaltAnswer, 31-myBit);
  534.  
  535. /***********************/
  536. /** 16 BITS SUPPORT ? **/
  537. /***********************/
  538.  
  539.     myBit = gestalt16BitSoundIO;
  540.     Audio16 = BitTst( &gestaltAnswer, 31-myBit);
  541.  
  542. /****************************/
  543. /** SOUND MANAGER >= 3.0 ? **/
  544. /****************************/
  545.  
  546.     nVers = SndSoundManagerVersion();
  547.     NewSoundManager31 = false;    NewSoundManager = false;
  548.     if( nVers.majorRev >= 3 && nVers.minorAndBugRev >= 0x10)
  549.     {
  550.         NewSoundManager31 = true;
  551.         NewSoundManager = true;
  552.     }
  553.     else if( nVers.majorRev >= 3) NewSoundManager = true;
  554.     
  555.     Init->numChn            = 4;
  556.     Init->surround            = false;
  557.     Init->repeatMusic        = true;
  558.     Init->sysMemory            = false;
  559.     Init->MicroDelaySize    = 25;
  560.     Init->Reverb            = false;
  561.     Init->ReverbSize        = 100;
  562.     Init->ReverbStrength    = 20;
  563.     Init->oversampling        = 1;
  564.     
  565.     // STEREO OR MONO ?
  566.     
  567.     Init->outPutMode = DeluxeStereoOutPut;
  568.     
  569.     // RATE AND SIZE
  570.     
  571.     if( NewSoundManager == true)
  572.     {
  573.         Init->driverMode = SoundManagerDriver;
  574.         
  575.         ////////
  576.         
  577.         #if !TARGET_RT_MAC_CFM
  578.         if( NewSoundManager31)
  579.         {
  580.             OSErr    iErr;
  581.             
  582.             iErr = GetSoundOutputInfo( 0L, siSampleRate, &Init->outPutRate);            if(iErr) goto oldWay;
  583.             iErr = GetSoundOutputInfo( 0L, siSampleSize, (void*) &Init->outPutBits);    if(iErr) goto oldWay;
  584.         }
  585.         else
  586.         #endif
  587.         {
  588.              oldWay:;
  589.              
  590.             if( Audio16)            {    Init->outPutBits = 16;    Init->outPutRate = rate44khz;}
  591.             else                    {    Init->outPutBits = 8;    Init->outPutRate = rate22khz;}
  592.         }
  593.     }
  594.     else
  595.     {
  596.         Init->driverMode = SoundManagerDriver;
  597.         
  598.         Init->outPutBits = 8;
  599.         Init->outPutRate = rate22khz;
  600.     }
  601.     #else
  602.     
  603.     #endif
  604. }
  605.  
  606. OSErr MADCreateMicroDelay( MADDriverRec *intDriver)
  607. {
  608.     intDriver->MDelay = (intDriver->DriverSettings.MicroDelaySize * ( intDriver->DriverSettings.outPutRate >> 16) * intDriver->DriverSettings.oversampling) / 1000;
  609.     
  610.     return noErr;
  611. }
  612.  
  613. void MADDisposeReverb( MADDriverRec *intDriver)
  614. {
  615.     if( intDriver->DriverSettings.Reverb) DisposePtr( intDriver->ReverbPtr);
  616.     intDriver->ReverbPtr = 0L;
  617. }
  618.  
  619. OSErr MADCreateReverb( MADDriverRec *intDriver)
  620. {
  621.     long i;
  622.     
  623.     if( intDriver->DriverSettings.Reverb)
  624.     {
  625.         intDriver->RDelay = (intDriver->DriverSettings.ReverbSize * ( intDriver->DriverSettings.outPutRate >> 16L) * intDriver->DriverSettings.oversampling) / 1000L;
  626.         
  627.         switch( intDriver->DriverSettings.outPutBits)
  628.         {
  629.             case 8:
  630.                 intDriver->ReverbPtr = NewPtr( intDriver->RDelay * 2L);
  631.                 if( intDriver->ReverbPtr == 0L) return MADNeedMemory;
  632.                 for( i = 0; i < intDriver->RDelay * 2L; i++) intDriver->ReverbPtr[ i] = 0x80;
  633.             break;
  634.             
  635.             case 16:
  636.                 intDriver->ReverbPtr = NewPtrClear( intDriver->RDelay * 4L);
  637.                 if( intDriver->ReverbPtr == 0L) return MADNeedMemory;
  638.             break;
  639.         }
  640.     }
  641.     
  642.     return noErr;
  643. }
  644.  
  645. void MADDisposeDriverBuffer( MADDriverRec *intDriver)
  646. {
  647.     if( intDriver->IntDataPtr != 0L)    DisposePtr( intDriver->IntDataPtr);
  648.     intDriver->IntDataPtr = 0L;
  649. }
  650.  
  651. OSErr MADCreateDriverBuffer( MADDriverRec *intDriver)
  652. {
  653.     long BufSize = intDriver->ASCBUFFER;
  654.     
  655.     switch( intDriver->DriverSettings.outPutMode)
  656.     {
  657.     //    case MonoOutPut:            BufSize = BufSize;            break;
  658.     //    case StereoOutPut:            BufSize = BufSize*2L;        break;
  659.         case DeluxeStereoOutPut:    BufSize = BufSize*2L;        break;
  660.     }
  661.     
  662.     switch( intDriver->DriverSettings.outPutBits)
  663.     {
  664.         case 16:                                    BufSize = BufSize*2L;        break;
  665.     }
  666.     
  667.     intDriver->IntDataPtr = MADNewPtr( BufSize, intDriver->lib);
  668.     if( intDriver->IntDataPtr == 0L) return MADNeedMemory;
  669.     
  670.     intDriver->BufSize            = BufSize;
  671.     intDriver->OscilloWavePtr    = intDriver->IntDataPtr;
  672.     intDriver->OscilloWaveSize    = BufSize / intDriver->DriverSettings.oversampling;
  673.     
  674.     return noErr;
  675. }
  676.  
  677. OSErr MADCreateTiming( MADDriverRec *intDriver)
  678. {
  679.     /*************************/
  680.     /**     Rate table      **/
  681.     /*************************/
  682.     
  683.     intDriver->VSYNC    = ((intDriver->DriverSettings.outPutRate>>16) * 125L * intDriver->DriverSettings.oversampling) / (50L);
  684.     intDriver->trackDiv = intDriver->DriverSettings.numChn;
  685. //    if( intDriver->DriverSettings.outPutMode == StereoOutPut) intDriver->trackDiv /= 2;
  686.  
  687.     return noErr;
  688. }
  689.  
  690.  
  691. OSErr MADChangeDriverSettings( MADDriverSettings    *DriverInitParam, MADDriverRec** returnDriver)
  692. {
  693.     MADMusic    *music;
  694.     Boolean        playing;
  695.     OSErr        err;
  696.     long        fullTime, curTime;
  697.     
  698.     music = (*returnDriver)->curMusic;
  699.     playing = (*returnDriver)->Reading;
  700.     
  701.     MADGetMusicStatus( *returnDriver, &fullTime, &curTime);
  702.     
  703.     if( (err = MADStopDriver( *returnDriver)) != noErr) return err;
  704.     if( (err = MADDisposeDriver( *returnDriver)) != noErr) return err;
  705.     
  706.     if( (err = MADCreateDriver( DriverInitParam, (*returnDriver)->lib, returnDriver)) != noErr) return err;
  707.     
  708.     if( (err = MADStartDriver( *returnDriver)) != noErr) return err;
  709.     
  710.     if( music)
  711.     {
  712.         MADAttachDriverToMusic( *returnDriver, music);
  713.         
  714.         MADSetMusicStatus( *returnDriver, 0, fullTime, curTime);
  715.         
  716.         if( playing) MADPlayMusic( *returnDriver);
  717.     }
  718.     
  719.     return noErr;
  720. }
  721.  
  722. void OpenMIDIHardware();
  723.  
  724. OSErr MADCreateDriver( MADDriverSettings    *DriverInitParam, MADLibrary *lib, MADDriverRec** returnDriver)
  725. {
  726.     OSErr                     theErr;
  727.     long                    i;
  728.     MADDriverRec*            MDriver;
  729.     
  730.     *returnDriver = 0L;
  731.         
  732.     /*************************/
  733.     /** Paramaters checking **/
  734.     /*************************/
  735.     
  736.     theErr = noErr;
  737.     
  738.     if( DriverInitParam->numChn % 2 != 0)
  739.     {
  740.         DriverInitParam->numChn /= 2;
  741.         DriverInitParam->numChn *= 2;
  742.     //    DriverInitParam->numChn++;
  743.     }
  744.     if( DriverInitParam->numChn < 2) theErr = MADParametersErr;
  745.     if( DriverInitParam->numChn > MAXTRACK) theErr = MADParametersErr;
  746.     
  747.     if( DriverInitParam->outPutBits != 8 && DriverInitParam->outPutBits != 16) theErr = MADParametersErr;
  748.     
  749.     if( DriverInitParam->outPutRate < rate5khz) theErr = MADParametersErr;
  750.     if( DriverInitParam->outPutRate > rate48khz) theErr = MADParametersErr;
  751.     
  752.     if( DriverInitParam->outPutMode != DeluxeStereoOutPut &&
  753.         DriverInitParam->outPutMode != PolyPhonic) theErr = MADParametersErr;
  754.     
  755.     if( DriverInitParam->driverMode != SoundManagerDriver &&
  756.         DriverInitParam->driverMode != MIDISoundDriver &&
  757.         DriverInitParam->driverMode != BeOSSoundDriver &&
  758.         DriverInitParam->driverMode != DirectSound95NT &&
  759.         DriverInitParam->driverMode != Wave95NT &&
  760.         DriverInitParam->driverMode != NoHardwareDriver) theErr = MADParametersErr;
  761.     
  762.     if( DriverInitParam->MicroDelaySize < 0)         theErr = MADParametersErr;
  763.     if( DriverInitParam->MicroDelaySize > 1000)     theErr = MADParametersErr;
  764.     
  765.     if( DriverInitParam->Reverb)
  766.     {
  767.         if( DriverInitParam->ReverbSize < 25)             theErr = MADParametersErr;
  768.         if( DriverInitParam->ReverbSize > 1000)         theErr = MADParametersErr;
  769.         
  770.         if( DriverInitParam->ReverbStrength < 0)         theErr = MADParametersErr;
  771.         if( DriverInitParam->ReverbStrength > 70)         theErr = MADParametersErr;
  772.     }
  773.     
  774.     if( DriverInitParam->oversampling < 1)    theErr = MADParametersErr;
  775.     if( DriverInitParam->oversampling > 20)    theErr = MADParametersErr;
  776.     
  777.     if( theErr != noErr)
  778.     {
  779.         return theErr;
  780.     }
  781.     
  782.     /*************************/
  783.     
  784.     #if defined(powerc) || defined (__powerc)
  785.     #else
  786.     DriverInitParam->oversampling = 1;            // We do NOT support oversampling on NON-64bits processor
  787.     #endif
  788.     
  789.     
  790.     
  791.     if( lib->sysMemory) MDriver = (MADDriverRec*) NewPtrSysClear( sizeof( MADDriverRec));
  792.     else MDriver = (MADDriverRec*) NewPtrClear( sizeof( MADDriverRec));
  793.     
  794.     MDriver->lib = lib;
  795.     MDriver->curMusic = 0L;
  796.     
  797.     MDriver->Reading = false;
  798.     MDriver->wasReading = false;
  799.     
  800.     theErr = MADStopDriver( MDriver);
  801.     if( theErr != noErr) return theErr;
  802.     
  803.     theErr = MADDisposeDriver( MDriver);
  804.     if( theErr != noErr) return theErr;
  805.     
  806.     theErr = MADCreateVibrato( MDriver);
  807.     if( theErr != noErr) return theErr;
  808.     
  809.     for( i = 0; i < MAXTRACK; i++) MDriver->Active[ i] = true;
  810.     
  811.     MDriver->DriverSettings        = *DriverInitParam;
  812.     MDriver->musicEnd                = false;
  813.     MDriver->Reading                = false;
  814.     MDriver->JumpToNextPattern    = true;
  815.     MDriver->smallcounter            = 128;            // Start immediately
  816.     MDriver->BufCounter            = 0;
  817.     MDriver->BytesToGenerate        = 0;
  818.     MDriver->speed                = 6;
  819.     MDriver->finespeed            = 125;
  820.     MDriver->VExt                    = 80;
  821.     MDriver->FreqExt                = 80;
  822.     MDriver->VolGlobal            = 64;
  823.     MDriver->SendMIDIClockData    = false;
  824.     MDriver->MODMode            = false;
  825.     MDriver->XMLinear            = false;
  826.     
  827.     MDriver->MIN_PITCH            = GetOldPeriod( NUMBER_NOTES-1, NOFINETUNE, MDriver);    //
  828.     MDriver->MAX_PITCH            = GetOldPeriod( 0, NOFINETUNE, MDriver);
  829.  
  830.     MDriver->MOD_MIN_PITCH        = GetOldPeriod( 24 + 4*12 -1, NOFINETUNE, MDriver);
  831.     MDriver->MOD_MAX_PITCH        = GetOldPeriod( 24, NOFINETUNE, MDriver);
  832.     
  833.     MADCleanDriver( MDriver);
  834.     
  835.     /*************************/
  836.     /**     Driver MODE        **/
  837.     /*************************/
  838.     
  839.     switch( MDriver->DriverSettings.driverMode)
  840.     {
  841.         #ifdef _MAC_H
  842.         case MIDISoundDriver:
  843.             MDriver->ASCBUFFER = 370;
  844.             MDriver->ASCBUFFER = (MDriver->ASCBUFFER * (MDriver->DriverSettings.outPutRate>>16L) * MDriver->DriverSettings.oversampling) / 22254L;
  845.             MDriver->ASCBUFFER /= 2;
  846.             MDriver->ASCBUFFER *= 2;
  847.             if( MDriver->ASCBUFFER < 370) MDriver->ASCBUFFER = 370;
  848.         break;
  849.         #endif
  850.         
  851.         case SoundManagerDriver:
  852.             MDriver->ASCBUFFER = 370;
  853.             MDriver->ASCBUFFER = (MDriver->ASCBUFFER * (MDriver->DriverSettings.outPutRate>>16L) * MDriver->DriverSettings.oversampling) / 22254L;
  854.             MDriver->ASCBUFFER /= 2;
  855.             MDriver->ASCBUFFER *= 2;
  856.             if( MDriver->ASCBUFFER < 370) MDriver->ASCBUFFER = 370;
  857.         break;
  858.         
  859.         case NoHardwareDriver:
  860.             MDriver->ASCBUFFER = 1024L * MDriver->DriverSettings.oversampling;
  861.         break;
  862.         
  863.         case BeOSSoundDriver:
  864.             MDriver->ASCBUFFER = 1024L * MDriver->DriverSettings.oversampling;
  865.             
  866.             if( MDriver->DriverSettings.outPutBits == 8) MDriver->ASCBUFFER *= 2L;
  867.         break;
  868.         
  869.         case DirectSound95NT:
  870.             MDriver->ASCBUFFER = 7500L * MDriver->DriverSettings.oversampling;
  871.         break;
  872.         
  873.         case Wave95NT:
  874.             MDriver->ASCBUFFER = 7500L * MDriver->DriverSettings.oversampling;
  875.         break;
  876.         
  877.         default:
  878.             return MADParametersErr;
  879.         break;
  880.     }
  881.     
  882.     MDriver->ASCBUFFERReal = MDriver->ASCBUFFER / MDriver->DriverSettings.oversampling;
  883.     
  884.     theErr = MADCreateDriverBuffer( MDriver);        if( theErr != noErr) return theErr;
  885.     theErr = MADCreateTiming( MDriver);                if( theErr != noErr) return theErr;
  886.     theErr = MADCreateReverb( MDriver);                if( theErr != noErr) return theErr;
  887.     theErr = MADCreateVolumeTable(    MDriver);        if( theErr != noErr) return theErr;
  888.     
  889.     /**********************************************/
  890.     /**    Interruption - Hardware Support       **/
  891.     /**********************************************/
  892.     
  893.     switch( MDriver->DriverSettings.driverMode)
  894.     {
  895.         #ifdef _MIDIHARDWARE_
  896.         case MIDISoundDriver:
  897.         
  898.             OpenMIDIHardware();
  899.             
  900.         /*    MDriver->gOutNodeRefNum = -1;
  901.             OMSAddPort( 'SNPL', 'out ', omsPortTypeOutput, NULL, 0L, &MDriver->MIDIPortRefNum);*/
  902.             
  903.             theErr = InitDBSoundManager( MDriver, initStereo);
  904.             if( theErr != noErr) return theErr;
  905.         break;
  906.         #endif
  907.         
  908.         #ifdef _MAC_H
  909.         case SoundManagerDriver:
  910.             theErr = InitDBSoundManager( MDriver, initStereo);
  911.             if( theErr != noErr) return theErr;
  912.         break;
  913.         #endif
  914.         
  915.         #ifdef _INTEL_H
  916.         case DirectSound95NT:
  917.             
  918.             if( !DirectSoundInit( MDriver)) theErr = MADUnknowErr;
  919.             if( theErr != noErr) return theErr;
  920.             
  921.         break;
  922.         
  923.         case Wave95NT:
  924.             if( !W95_Init( MDriver)) theErr = MADUnknowErr;
  925.             if( theErr != noErr) return theErr;
  926.         break;
  927.         #endif
  928.         
  929.         case BeOSSoundDriver:
  930.         
  931.         break;
  932.         
  933.         case NoHardwareDriver:
  934.         
  935.         break;
  936.     }
  937.     
  938.     *returnDriver = MDriver;
  939.     
  940.     return noErr;
  941. }
  942.  
  943. OSErr MADDisposeDriver( MADDriverRec* MDriver)
  944. {
  945.     if( MDriver->IntDataPtr == 0L) return noErr;
  946.     
  947.     MDriver->Reading = false;
  948.     
  949.     MADCleanDriver( MDriver);
  950.     
  951.     switch( MDriver->DriverSettings.driverMode)
  952.     {
  953.         #ifdef _MAC_H
  954.         case MIDISoundDriver:
  955.             AllNoteOff( MDriver);
  956.             DBSndClose( MDriver);
  957.         break;
  958.         
  959.         case SoundManagerDriver:
  960.             DBSndClose( MDriver);
  961.         break;
  962.         #endif
  963.         
  964.         #ifdef _INTEL_H
  965.         case DirectSound95NT:
  966.             DirectSoundClose( MDriver);
  967.         break;
  968.         
  969.         case Wave95NT:
  970.             W95_Exit( MDriver);
  971.         break;
  972.         #endif
  973.     }
  974.     
  975.     MADDisposeDriverBuffer( MDriver);            if( MemError()) return MADUnknowErr;
  976.     MADDisposeVolumeTable( MDriver);            if( MemError()) return MADUnknowErr;
  977.     MADDisposeReverb( MDriver);                    if( MemError()) return MADUnknowErr;
  978.     
  979.     DisposePtr( (Ptr) MDriver);
  980.     
  981.     return noErr;
  982. }
  983.  
  984. OSErr MADInitLibrary( char *PlugsFolderName, Boolean sysMemory, MADLibrary **lib)
  985. {
  986.     long     i, mytab[ 12] =
  987.     {
  988.         1712L*16L,1616L*16L,1524L*16L,1440L*16L,1356L*16L,1280L*16L,
  989.         1208L*16L,1140L*16L,1076L*16L,1016L*16L,960L*16L,907L*16L
  990.     };
  991.     
  992.     if( sysMemory) *lib = (MADLibrary*) NewPtrSysClear( sizeof( MADLibrary));
  993.     else *lib = (MADLibrary*) NewPtrClear( sizeof( MADLibrary));
  994.     
  995.     if( *lib == 0L) return MADNeedMemory;
  996.     
  997.     (*lib)->IDType = 'MADD';
  998.     (*lib)->sysMemory = sysMemory;
  999.     
  1000.     for( i = 0; i < 12; i++)
  1001.     {
  1002.         (*lib)->mytab[ i] = mytab[ i];
  1003.     }
  1004.     
  1005.     MInitImportPlug( *lib, PlugsFolderName);
  1006.     
  1007.     return noErr;
  1008. }
  1009.  
  1010. OSErr MADDisposeLibrary( MADLibrary *MLibrary)
  1011. {
  1012.     if( MLibrary != 0L)
  1013.     {
  1014.         CloseImportPlug( MLibrary);
  1015.         MLibrary->IDType = 'XXXX';
  1016.         
  1017.         DisposePtr( (Ptr) MLibrary);
  1018.         MLibrary = 0L;
  1019.     }
  1020.     return noErr;
  1021. }
  1022.  
  1023. OSErr MADAttachDriverToMusic( MADDriverRec *driver, MADMusic *music)
  1024. {
  1025.     if( !driver) return -1;
  1026.     if( !music) return -1;
  1027.     
  1028.     driver->curMusic = music;
  1029.     driver->VolGlobal    = music->header->generalVol;        if( driver->VolGlobal <= 0) driver->VolGlobal = 64;
  1030.     driver->VExt        = music->header->generalSpeed;        if( driver->VExt <= 0) driver->VExt = 80;
  1031.     driver->FreqExt        = music->header->generalPitch;        if( driver->FreqExt <= 0) driver->FreqExt = 80;
  1032.     
  1033.     if( music->header->MODMode) driver->MODMode = true;
  1034.     else driver->MODMode = false;
  1035.     
  1036.     if( music->header->XMLinear) driver->XMLinear = true;
  1037.     else driver->XMLinear = false;
  1038.     
  1039.     MADReset( driver);
  1040.     UpdateTracksNumber( driver);
  1041.     
  1042.     return noErr;
  1043. }
  1044.  
  1045. #ifdef _MAC_H
  1046. OSErr MADLoadMusicRsrc( MADMusic **music, OSType IDName, short    IDNo)
  1047. {
  1048.     Handle    theRsrc;
  1049.     OSErr    theErr;
  1050.     
  1051.     SetResLoad( false);
  1052.     theRsrc = GetResource( IDName, IDNo);
  1053.     if( theRsrc == 0L) return MADReadingErr;
  1054.     
  1055.     theErr = MADReadMAD( music, 0, MADRsrcType, theRsrc, 0L);
  1056.     if( theErr != noErr)
  1057.     {
  1058.         ReleaseResource( theRsrc);
  1059.         SetResLoad( true);
  1060.         
  1061.         MADDisposeMusic( music, 0L);
  1062.         return theErr;
  1063.     }
  1064.     
  1065.     ReleaseResource( theRsrc);
  1066.     SetResLoad( true);
  1067.     
  1068.     return noErr;
  1069. }
  1070. #endif
  1071.  
  1072. OSErr MADLoadMusicPtr( MADMusic **music, Ptr myPtr)
  1073. {
  1074.     OSErr    theErr;
  1075.     
  1076. //    MADDisposeMusic( music);
  1077.     
  1078.     theErr = MADReadMAD( music, 0, MADPtrType, 0L, myPtr);
  1079.     if( theErr != noErr) return theErr;
  1080.     
  1081.     return noErr;
  1082. }
  1083.  
  1084. OSErr MADLoadMADFileCString( MADMusic **music, Ptr fName)
  1085. {
  1086. OSErr        theErr;
  1087. UNFILE    srcFile;
  1088.  
  1089. //    MADDisposeMusic( music);
  1090.     
  1091.     srcFile = iFileOpen( fName);
  1092.     if( srcFile == 0L) return MADReadingErr;
  1093.     
  1094.     theErr = MADReadMAD( music, srcFile, MADFileType, 0L, 0L);
  1095.     if( theErr != noErr)
  1096.     {
  1097.         iClose( srcFile);
  1098.         MADDisposeMusic( music, 0L);
  1099.         return theErr;
  1100.     }
  1101.     
  1102.     iClose( srcFile);
  1103.     
  1104.     return( noErr);
  1105. }
  1106.  
  1107. #ifdef _MAC_H
  1108. OSErr MADSetHardwareVolume( long vol)
  1109. {
  1110. Point            tempL;
  1111. long            *tL;
  1112. NumVersion        nVers;
  1113. Boolean            NewSoundManager;
  1114.  
  1115.     if(vol > 64) vol = 64;
  1116.     if(vol < 0) vol = 0;
  1117.     
  1118.     nVers = SndSoundManagerVersion();
  1119. //    BlockMoveData( &tt, &nVers, 4);
  1120.     if( nVers.majorRev >= 3) NewSoundManager = true;
  1121.     else NewSoundManager = false;
  1122.     
  1123.     if( vol > 0)
  1124.     {
  1125.         if( NewSoundManager)
  1126.         {
  1127.             tempL.v = vol * 4;
  1128.             tempL.h = vol * 4;
  1129.             
  1130.             tL = (long*) &tempL;
  1131.             
  1132.             SetDefaultOutputVolume( *tL);
  1133.         }
  1134.         #if defined(powerc) || defined (__powerc)
  1135.         #else
  1136.         else
  1137.         {
  1138.         /*    #if GENERATINGCFM
  1139.             #else
  1140.             vol = (vol * 7L) / 64L;
  1141.             SetSoundVol( vol);
  1142.             #endif    */
  1143.         }
  1144.         #endif
  1145.     }
  1146.     
  1147.     return noErr;
  1148. }
  1149.  
  1150. long MADGetHardwareVolume()
  1151. {
  1152. Point            tempL;
  1153. NumVersion        nVers;
  1154. Boolean            NewSoundManager;
  1155. long            vol;
  1156.     
  1157.     nVers = SndSoundManagerVersion();
  1158. //    BlockMoveData( &tt, &nVers, 4);
  1159.     if( nVers.majorRev >= 3) NewSoundManager = true;
  1160.     else NewSoundManager = false;
  1161.     
  1162.     if( NewSoundManager)
  1163.     {
  1164.         GetDefaultOutputVolume( (long*) &tempL);
  1165.         
  1166.         vol = tempL.v / 4L;
  1167.     }
  1168.     #if defined(powerc) || defined (__powerc)
  1169.     #else
  1170.     else
  1171.     {
  1172.     /*    #if GENERATINGCFM
  1173.         #else
  1174.         short shortVol;
  1175.         
  1176.         GetSoundVol( &shortVol);
  1177.         
  1178.         vol = shortVol;
  1179.         vol = (64L * vol) / 7L;
  1180.         #endif*/
  1181.     }
  1182.     #endif
  1183.     
  1184.     if(vol > 64) vol = 64;
  1185.     if(vol < 0) vol = 0;
  1186.  
  1187.     return vol;
  1188. }
  1189.  
  1190. OSErr MADLoadMusicFSpFile( MADLibrary *lib, MADMusic **music, char *plugType, FSSpec *theSpec)
  1191. {
  1192. OSErr                        iErr;
  1193. FSSpec                    saved;
  1194.  
  1195.     HGetVol( 0L, &saved.vRefNum, &saved.parID);
  1196.     HSetVol( 0L, theSpec->vRefNum , theSpec->parID);
  1197.     p2cstr( theSpec->name);
  1198.     
  1199.     if( !MADstrcmp( "MADI", plugType))     iErr = MADLoadMADFileCString( music, (Ptr) theSpec->name);
  1200.     else                                iErr = MADLoadMusicFileCString( lib, music, plugType, (Ptr) theSpec->name);
  1201.     
  1202.     c2pstr( (Ptr) theSpec->name);
  1203.     HSetVol( 0L, saved.vRefNum , saved.parID);
  1204.     
  1205.     return iErr;
  1206. }
  1207.  
  1208. OSErr MADLoadMusicFilePString( MADLibrary *lib, MADMusic **music, char *plugType, Str255 fName)
  1209. {
  1210. OSErr                iErr;
  1211.  
  1212.     p2cstr( fName);
  1213.     
  1214.     if( !MADstrcmp( "MADI", plugType))     iErr = MADLoadMADFileCString(  music, (Ptr) fName);
  1215.     else                                iErr = MADLoadMusicFileCString( lib, music, plugType, (Ptr) fName);
  1216.     
  1217.     c2pstr( (Ptr) fName);
  1218.     
  1219.     return iErr;
  1220. }
  1221.  
  1222. /*
  1223. OSErr MADCopyCurrentPartition( MADMusic *aPartition)
  1224. {
  1225.     long    i, x;
  1226.     
  1227.     if( MDriver->header == 0L) return -1;
  1228.     if( aPartition == 0L) return -1;
  1229.     
  1230.     aPartition->header = (MADSpec*) MADNewPtr( sizeof( MADSpec), &MDriver->DriverSettings);
  1231.     if( aPartition->header == 0L) return -1;
  1232.     
  1233.     BlockMoveData( MDriver->header, aPartition->header, sizeof( MADSpec));
  1234.     
  1235.     for( i = 0; i < MDriver->header->numPat ; i++)
  1236.     {
  1237.         aPartition->partition[ i] = (PatData*) MADNewPtr( GetPtrSize( (Ptr) MDriver->partition[ i]), &MDriver->DriverSettings);
  1238.         if( aPartition->partition[ i] == 0L) return -1;
  1239.         
  1240.         BlockMoveData( MDriver->partition[ i], aPartition->partition[ i], GetPtrSize( (Ptr) MDriver->partition[ i]));
  1241.     }
  1242.     for( i = MDriver->header->numPat; i < MAXPATTERN ; i++) aPartition->partition[ i] = 0L;
  1243.     
  1244.     for( i = 0; i < MAXINSTRU ; i++)
  1245.     {
  1246.         for( x = 0; x < MDriver->header->fid[i].numSamples; x++)
  1247.         {
  1248.             sData    *dstData, *srcData = MDriver->sample[ i][ x];
  1249.             
  1250.             aPartition->sample[ i][ x] = dstData = (sData*) MADNewPtr( sizeof( sData), &MDriver->DriverSettings);
  1251.             if( dstData == 0L) return -1;
  1252.             
  1253.             BlockMoveData( srcData, dstData, sizeof( sData));
  1254.             
  1255.             dstData->data = MADNewPtr( srcData->size, &MDriver->DriverSettings);
  1256.             if( dstData->data == 0L) return -1;
  1257.             
  1258.             BlockMoveData( srcData->data, dstData->data, srcData->size);
  1259.         }
  1260.     }
  1261.     
  1262.     return noErr;
  1263. }*/
  1264.  
  1265. OSErr    MADMusicIdentifyFSp( MADLibrary *lib, char *type, FSSpec *theSpec)
  1266. {
  1267.     FSSpec    saved;
  1268.     OSErr        err;
  1269.     
  1270.     HGetVol( 0L, &saved.vRefNum, &saved.parID);
  1271.     HSetVol( 0L, theSpec->vRefNum, theSpec->parID);
  1272.     p2cstr( theSpec->name);
  1273.     
  1274.     err =  PPIdentifyFile( lib, type, (Ptr) theSpec->name);
  1275.     
  1276.     c2pstr( (Ptr) theSpec->name);
  1277.     HSetVol( 0L, saved.vRefNum, saved.parID);
  1278.     
  1279.     return err;
  1280. }
  1281.  
  1282. OSErr    MADMusicIdentifyPString( MADLibrary *lib, char *type, Str255 fName)
  1283. {
  1284.     OSErr    err;
  1285.     
  1286.     p2cstr( fName);
  1287.     err = PPIdentifyFile( lib, type, (Ptr) fName);
  1288.     c2pstr( (Ptr) fName);
  1289.     
  1290.     return err;
  1291. }
  1292. #endif
  1293.  
  1294. OSErr    MADMusicIdentifyCString( MADLibrary *lib, char *type, Ptr fName)
  1295. {
  1296.     OSErr    err;
  1297.     
  1298.     err = PPIdentifyFile( lib, type, fName);
  1299.     
  1300.     return err;
  1301. }
  1302.  
  1303. OSErr MADLoadMusicFileCString( MADLibrary *lib, MADMusic **music, char *plugType, Ptr fName)
  1304. {
  1305. OSErr            iErr;
  1306.  
  1307.     if( !MADstrcmp( "MADI", plugType)) iErr = MADLoadMADFileCString( music, fName);
  1308.     else
  1309.     {
  1310.         iErr = PPImportFile( lib, plugType, fName, music);
  1311.     }
  1312.     
  1313.     return iErr;
  1314. }
  1315.  
  1316. OSErr MADSetMusicStatus( MADDriverRec *MDriver, long minV, long maxV, long curV)
  1317. {
  1318.     short            i, x, y;
  1319.     Cmd                *aCmd;
  1320.  
  1321.     float            timeResult;
  1322.     long            time;
  1323.     long            speed, finespeed, fullTime, curTime;
  1324.     long            dstTime;
  1325.     
  1326.     if( MDriver == 0L)
  1327.     {
  1328.         return -1; 
  1329.     }
  1330.     
  1331.     if( MDriver->curMusic == 0L)
  1332.     {
  1333.         return -1; 
  1334.     }
  1335.     
  1336.     if( MDriver->curMusic->header == 0L)
  1337.     {
  1338.         return -1; 
  1339.     }
  1340.     
  1341.     MADGetMusicStatus( MDriver, &fullTime, &curTime);
  1342.     
  1343.     curV -= minV;
  1344.     maxV -= minV;
  1345.     
  1346.     dstTime            = (curV * fullTime)/maxV;
  1347.     timeResult        = 0;
  1348.     time            = 0;
  1349.     speed            = MDriver->curMusic->header->speed;
  1350.     finespeed        = MDriver->curMusic->header->tempo;
  1351.     
  1352.     for( i = 0; i < MDriver->curMusic->header->numPointers; i++)
  1353.     {
  1354.         for( x = 0; x < MDriver->curMusic->partition[ MDriver->curMusic->header->oPointers[ i]]->header.size; x++)
  1355.         {
  1356.             time ++;
  1357.             
  1358.             if( dstTime <= timeResult + ((float) (time * 125L * speed * 60L)) / ((float) (50L * finespeed)))
  1359.             {
  1360.                 MDriver->PL = i;
  1361.                 MDriver->PartitionReader = x;
  1362.                 MDriver->Pat = MDriver->curMusic->header->oPointers[ i];
  1363.                 
  1364.                 MADCheckSpeed( MDriver->curMusic, MDriver);
  1365.                 
  1366.                 MADPurgeTrack( MDriver);
  1367.                 
  1368.                 return noErr;
  1369.             }
  1370.             
  1371.             for( y = 0; y <  MDriver->curMusic->header->numChn; y++)
  1372.             {
  1373.                 aCmd = GetMADCommand( x, y, MDriver->curMusic->partition[ MDriver->curMusic->header->oPointers[ i]]);
  1374.                                 
  1375.                 /** SpeedE **/
  1376.                 
  1377.                 if( aCmd->cmd == speedE)
  1378.                 {
  1379.                     /** Compute time for this interval **/
  1380.  
  1381.                     timeResult += ((float) (time * 125L * speed * 60L)) / ((float) (50L * finespeed));
  1382.                     time = 0;
  1383.                     
  1384.                     /************************************/
  1385.                     
  1386.                     if( aCmd->arg < 32)
  1387.                     {
  1388.                         if( aCmd->arg != 0) speed = aCmd->arg;
  1389.                     }
  1390.                     else
  1391.                     {
  1392.                         if( aCmd->arg != 0) finespeed = aCmd->arg;
  1393.                     }
  1394.                 }
  1395.                 
  1396.                 /** SkipE **/
  1397.                 
  1398.                 if( aCmd->cmd == skipE)
  1399.                 {
  1400.                     for( ; x < MDriver->curMusic->partition[ MDriver->curMusic->header->oPointers[ i]]->header.size; x++)
  1401.                     {
  1402.                     }
  1403.                 }
  1404.             }
  1405.         }
  1406.     }
  1407.     
  1408.     return -1;
  1409. }
  1410.  
  1411. OSErr MADGetMusicStatus( MADDriverRec *MDriver, long *fullTime, long *curTime)
  1412. {
  1413.     short            i, x, y;
  1414.     Cmd                *aCmd;
  1415.  
  1416.     float            timeResult;
  1417.     long            time;
  1418.     long            speed, finespeed;
  1419.     
  1420.     if( MDriver == 0L)
  1421.     {
  1422.         *curTime = 0;
  1423.         *fullTime = 1;
  1424.         
  1425.         return -1; 
  1426.     }
  1427.     
  1428.     if( MDriver->curMusic == 0L)
  1429.     {
  1430.         *curTime = 0;
  1431.         *fullTime = 1;
  1432.         
  1433.         return -1; 
  1434.     }
  1435.     
  1436.     if( MDriver->curMusic->header == 0L)
  1437.     {
  1438.         *curTime = 0;
  1439.         *fullTime = 1;
  1440.         
  1441.         return -1; 
  1442.     }
  1443.     
  1444.     timeResult    = 0;
  1445.     time                = 0;
  1446.     speed                = MDriver->curMusic->header->speed;
  1447.     finespeed            = MDriver->curMusic->header->tempo;
  1448.     
  1449.     *curTime        = -1;
  1450.     
  1451.     for( i = 0; i < MDriver->curMusic->header->numPointers; i++)
  1452.     {
  1453.         for( x = 0; x < MDriver->curMusic->partition[ MDriver->curMusic->header->oPointers[ i]]->header.size; x++)
  1454.         {
  1455.             time ++;
  1456.             
  1457.             if( i == MDriver->PL    &&
  1458.                 x == MDriver->PartitionReader)
  1459.                 {
  1460.                     *curTime = timeResult + ((float) (time * 125L * speed * 60L)) / ((float) (50L * finespeed));
  1461.                 }
  1462.             
  1463.             for( y = 0; y <  MDriver->curMusic->header->numChn; y++)
  1464.             {
  1465.                 aCmd = GetMADCommand( x, y, MDriver->curMusic->partition[ MDriver->curMusic->header->oPointers[ i]]);
  1466.                                 
  1467.                 /** SpeedE **/
  1468.                 
  1469.                 if( aCmd->cmd == speedE)
  1470.                 {
  1471.                     /** Compute time for this interval **/
  1472.  
  1473.                     timeResult += ((float) (time * 125L * speed * 60L)) / ((float) (50L * finespeed));
  1474.                     time = 0;
  1475.                     
  1476.                     /************************************/
  1477.                     
  1478.                     if( aCmd->arg < 32)
  1479.                     {
  1480.                         if( aCmd->arg != 0) speed = aCmd->arg;
  1481.                     }
  1482.                     else
  1483.                     {
  1484.                         if( aCmd->arg != 0) finespeed = aCmd->arg;
  1485.                     }
  1486.                 }
  1487.                 
  1488.                 /** SkipE **/
  1489.                 
  1490.                 if( aCmd->cmd == skipE)
  1491.                 {
  1492.                     for( ; x < MDriver->curMusic->partition[ MDriver->curMusic->header->oPointers[ i]]->header.size; x++)
  1493.                     {
  1494.                         if( i == MDriver->PL    &&
  1495.                                 x == MDriver->PartitionReader)
  1496.                         {
  1497.                             *curTime = timeResult + ((float) (time * 125L * speed * 60L)) / ((float) (50L * finespeed));
  1498.                         }
  1499.                     }
  1500.                 }
  1501.             }
  1502.         }
  1503.     }
  1504.     
  1505.     timeResult += ((float) (time * 125L * speed * 60L)) / ((float) (50L * finespeed));
  1506.     
  1507.     *fullTime = timeResult;
  1508.     
  1509.     if( *curTime == -1) *curTime = *fullTime;
  1510.     
  1511.     return noErr;
  1512. }
  1513.  
  1514. OSErr MADReadMAD( MADMusic **music, UNFILE srcFile, short InPutType, Handle MADRsrc, Ptr MADPtr)
  1515. {
  1516. short                     i, x;
  1517. long                     inOutCount, OffSetToSample;
  1518. OSErr                    theErr = noErr;
  1519. PatHeader                tempPatHeader;
  1520. PatData*                tempPat;
  1521. MADMusic                 *MDriver;
  1522.  
  1523. *music = 0L;
  1524.  
  1525. MDriver = (MADMusic*) NewPtrClear( sizeof( MADMusic));
  1526. if( !MDriver) return -1L;
  1527.  
  1528. MDriver->musicUnderModification = false;
  1529.  
  1530. /**** HEADER ****/
  1531. inOutCount = sizeof( MADSpec);
  1532.  
  1533. MDriver->header = (MADSpec*) NewPtr( inOutCount);
  1534. if( MDriver->header == 0L)
  1535. {
  1536.     DisposePtr( (Ptr) MDriver);
  1537.     return MADNeedMemory;
  1538. }
  1539.  
  1540. switch( InPutType)
  1541. {
  1542.     #ifdef _MAC_H
  1543.     case MADRsrcType:
  1544.         OffSetToSample = 0L;
  1545.         ReadPartialResource( MADRsrc, 0L, MDriver->header, inOutCount);
  1546.         OffSetToSample += inOutCount;
  1547.     break;
  1548.     #endif
  1549.     
  1550.     case MADFileType:
  1551.         if( iRead( inOutCount, (Ptr) MDriver->header, srcFile)) theErr = MADIncompatibleFile;
  1552.     break;
  1553.     
  1554.     case MADPtrType:
  1555.         OffSetToSample = 0L;
  1556.         BlockMoveData( MADPtr, MDriver->header, inOutCount);
  1557.         OffSetToSample += inOutCount;
  1558.     break;
  1559. }
  1560.  
  1561. MOT32( &MDriver->header->MAD);
  1562. MOT16( &MDriver->header->speed);
  1563. MOT16( &MDriver->header->tempo);
  1564.  
  1565. if( MDriver->header->MAD != 'MADI' || MDriver->header->numInstru > MAXINSTRU)
  1566. {
  1567.     DisposePtr( (Ptr) MDriver->header);
  1568.     DisposePtr( (Ptr) MDriver);
  1569.     return MADIncompatibleFile;
  1570. }
  1571.  
  1572. /**** PARTITION ****/
  1573. for( i = MDriver->header->numPat; i < MAXPATTERN; i++) MDriver->partition[ i] = 0L;
  1574.  
  1575. for( i = 0; i < MDriver->header->numPat; i++)
  1576. {
  1577.     /** Lecture du header de la partition **/
  1578.     inOutCount = sizeof( PatHeader);
  1579.         
  1580.     switch( InPutType)
  1581.     {
  1582.         #ifdef _MAC_H
  1583.         case MADRsrcType:
  1584.             ReadPartialResource( MADRsrc, OffSetToSample, &tempPatHeader, inOutCount);
  1585.             //    OffSetToSample += inOutCount; ON LE RELIT APRES !!!!!!
  1586.         break;
  1587.         #endif
  1588.         
  1589.         case MADFileType:
  1590.             if( iRead( inOutCount, (Ptr) &tempPatHeader, srcFile)) theErr = MADIncompatibleFile;
  1591.             iSeekCur( -sizeof( PatHeader), srcFile);
  1592.         break;
  1593.         
  1594.         case MADPtrType:
  1595.             BlockMoveData( MADPtr + OffSetToSample, &tempPatHeader, inOutCount);
  1596.         break;
  1597.     }
  1598.     
  1599.     MOT32( &tempPatHeader.size);
  1600.     MOT32( &tempPatHeader.compMode);
  1601.     MOT32( &tempPatHeader.patBytes);
  1602.     MOT32( &tempPatHeader.unused2);
  1603.     
  1604.     /*************************************************/
  1605.     /** Lecture du header + contenu de la partition **/
  1606.     /*************************************************/
  1607.     
  1608.     switch( tempPatHeader.compMode)
  1609.     {
  1610.         case 'MAD1':
  1611.             inOutCount = sizeof( PatHeader) + tempPatHeader.patBytes;
  1612.         break;
  1613.         
  1614.         default:
  1615.         case 'NONE':
  1616.             inOutCount = sizeof( PatHeader) + MDriver->header->numChn * tempPatHeader.size * sizeof( Cmd);
  1617.         break;
  1618.     }
  1619.     
  1620.     MDriver->partition[ i] = ( PatData*) NewPtr( inOutCount);
  1621.     if( MDriver->partition[ i] == 0L)
  1622.     {
  1623.         for( x = 0; x < i; x++)
  1624.         {
  1625.             if( MDriver->partition[ x] != 0L)    DisposePtr( (Ptr) MDriver->partition[ x]);
  1626.         }
  1627.         DisposePtr( (Ptr) MDriver->header);
  1628.         DisposePtr( (Ptr) MDriver);
  1629.         
  1630.         return MADNeedMemory;
  1631.     }
  1632.     switch( InPutType)
  1633.     {
  1634.         #ifdef _MAC_H
  1635.         case MADRsrcType:
  1636.             ReadPartialResource( MADRsrc, OffSetToSample, MDriver->partition[ i], inOutCount);
  1637.             OffSetToSample += inOutCount;
  1638.         break;
  1639.         #endif
  1640.         
  1641.         case MADFileType:
  1642.             if( iRead( inOutCount, (Ptr) MDriver->partition[ i], srcFile)) theErr = MADIncompatibleFile;
  1643.         break;
  1644.         
  1645.         case MADPtrType:
  1646.             BlockMoveData( MADPtr + OffSetToSample, MDriver->partition[ i], inOutCount);
  1647.             OffSetToSample += inOutCount;
  1648.         break;
  1649.     }
  1650.     
  1651.     MOT32( &MDriver->partition[ i]->header.size);
  1652.     MOT32( &MDriver->partition[ i]->header.compMode);
  1653.     MOT32( &MDriver->partition[ i]->header.patBytes);
  1654.     MOT32( &MDriver->partition[ i]->header.unused2);
  1655.     
  1656.     if( MDriver->partition[ i]->header.compMode == 'MAD1')
  1657.     {
  1658.         tempPat = DecompressPartitionMAD1( MDriver, MDriver->partition[ i]);
  1659.         
  1660.         if( tempPat == 0L)
  1661.         {
  1662.             for( x = 0; x < i; x++)
  1663.             {
  1664.                 if( MDriver->partition[ x] != 0L)    DisposePtr( (Ptr) MDriver->partition[ x]);
  1665.             }
  1666.             DisposePtr( (Ptr) MDriver->header);
  1667.             DisposePtr( (Ptr) MDriver);
  1668.             
  1669.             return MADNeedMemory;
  1670.         }
  1671.         
  1672.         DisposePtr( (Ptr) MDriver->partition[ i]);
  1673.         MDriver->partition[ i] = tempPat;
  1674.     }
  1675. }
  1676.  
  1677. /**** INSTRUMENTS ****/
  1678. MDriver->fid = ( InstrData*) NewPtrClear( sizeof( InstrData) * (long) MAXINSTRU);
  1679. if( !MDriver->fid)
  1680. {
  1681.     for( x = 0; x < MDriver->header->numPat; x++)
  1682.     {
  1683.         if( MDriver->partition[ x] != 0L)    DisposePtr( (Ptr) MDriver->partition[ x]);
  1684.     }
  1685.     DisposePtr( (Ptr) MDriver->header);
  1686.     DisposePtr( (Ptr) MDriver);
  1687.     
  1688.     return MADNeedMemory;
  1689. }
  1690.  
  1691. inOutCount = sizeof( InstrData) * (long) MDriver->header->numInstru;
  1692. switch( InPutType)
  1693. {
  1694.     #ifdef _MAC_H
  1695.     case MADRsrcType:
  1696.         ReadPartialResource( MADRsrc, OffSetToSample, MDriver->fid, inOutCount);
  1697.         OffSetToSample += inOutCount;
  1698.     break;
  1699.     #endif
  1700.     
  1701.     case MADFileType:
  1702.         if( iRead( inOutCount, (Ptr) MDriver->fid, srcFile)) theErr = MADIncompatibleFile;
  1703.     break;
  1704.     
  1705.     case MADPtrType:
  1706.         BlockMoveData( MADPtr + OffSetToSample, MDriver->fid, inOutCount);
  1707.         OffSetToSample += inOutCount;
  1708.     break;
  1709. }
  1710.  
  1711. for( i = MDriver->header->numInstru-1; i >= 0 ; i--)
  1712. {
  1713.     InstrData    *curIns = &MDriver->fid[ i];
  1714.     
  1715.     MOT16( &curIns->numSamples);
  1716.     MOT16( &curIns->firstSample);
  1717.     MOT16( &curIns->volFade);
  1718.     
  1719.     for( x = 0; x < 12; x++)
  1720.     {
  1721.         MOT16( &curIns->volEnv[ x].pos);
  1722.         MOT16( &curIns->volEnv[ x].val);
  1723.     }
  1724.     
  1725.     for( x = 0; x < 12; x++)
  1726.     {
  1727.         MOT16( &curIns->pannEnv[ x].pos);
  1728.         MOT16( &curIns->pannEnv[ x].val);
  1729.     }
  1730.     
  1731.     if( i != curIns->no)
  1732.     {
  1733.         MDriver->fid[ curIns->no] = *curIns;
  1734.         MADResetInstrument( curIns);
  1735.     }
  1736. }
  1737. MDriver->header->numInstru = MAXINSTRU;
  1738.  
  1739. /**** SAMPLES ****/
  1740. MDriver->sample = ( sData**) NewPtrClear( sizeof( sData*) * (long) MAXINSTRU * (long) MAXSAMPLE);
  1741. if( !MDriver->sample)
  1742. {
  1743.     for( x = 0; x < MAXINSTRU ; x++) MADKillInstrument( MDriver, x);
  1744.     
  1745.     for( x = 0; x < MDriver->header->numPat; x++)
  1746.     {
  1747.         if( MDriver->partition[ x] != 0L)    DisposePtr( (Ptr) MDriver->partition[ x]);
  1748.     }
  1749.     DisposePtr( (Ptr) MDriver->header);
  1750.     DisposePtr( (Ptr) MDriver);
  1751.     
  1752.     return MADNeedMemory;
  1753. }
  1754.  
  1755. for( i = 0; i < MAXINSTRU ; i++)
  1756. {
  1757.     for( x = 0; x < MDriver->fid[ i].numSamples ; x++)
  1758.     {
  1759.         sData     *curData;
  1760.         
  1761.         // ** Read Sample header **
  1762.         
  1763.         curData = MDriver->sample[ i*MAXSAMPLE + x] = (sData*) NewPtr( sizeof( sData));
  1764.         if( curData == 0L)
  1765.         {
  1766.             for( x = 0; x < MAXINSTRU ; x++) MADKillInstrument( MDriver, x);
  1767.             
  1768.             for( x = 0; x < MDriver->header->numPat; x++)
  1769.             {
  1770.                 if( MDriver->partition[ x] != 0L)    DisposePtr( (Ptr) MDriver->partition[ x]);
  1771.             }
  1772.             DisposePtr( (Ptr) MDriver->header);
  1773.             DisposePtr( (Ptr) MDriver);
  1774.             
  1775.             return MADNeedMemory;
  1776.         }
  1777.         
  1778.         inOutCount = sizeof( sData);
  1779.         
  1780.         switch( InPutType)
  1781.         {
  1782.             #ifdef _MAC_H
  1783.             case MADRsrcType:
  1784.                 ReadPartialResource( MADRsrc, OffSetToSample, curData, inOutCount);
  1785.                 OffSetToSample += inOutCount;
  1786.             break;
  1787.             #endif
  1788.             
  1789.             case MADFileType:
  1790.                 if( iRead( inOutCount, (Ptr) curData, srcFile)) theErr = MADIncompatibleFile;
  1791.             break;
  1792.             
  1793.             case MADPtrType:
  1794.                 BlockMoveData( MADPtr + OffSetToSample, curData, inOutCount);
  1795.                 OffSetToSample += inOutCount;
  1796.             break;
  1797.         }
  1798.         
  1799.         MOT32( &curData->size);
  1800.         MOT32( &curData->loopBeg);
  1801.         MOT32( &curData->loopSize);
  1802.         MOT16( &curData->c2spd);
  1803.         
  1804.         // ** Read Sample DATA
  1805.         
  1806.         curData->data = NewPtr( curData->size);
  1807.         if( curData->data == 0L)
  1808.         {
  1809.             for( x = 0; x < MAXINSTRU ; x++) MADKillInstrument( MDriver, x);
  1810.             
  1811.             for( x = 0; x < MDriver->header->numPat; x++)
  1812.             {
  1813.                 if( MDriver->partition[ x] != 0L)    DisposePtr( (Ptr) MDriver->partition[ x]);
  1814.             }
  1815.             DisposePtr( (Ptr) MDriver->header);
  1816.             DisposePtr( (Ptr) MDriver);
  1817.             
  1818.             return MADNeedMemory;
  1819.         }
  1820.         
  1821.         inOutCount = curData->size;
  1822.         switch( InPutType)
  1823.         {
  1824.             #ifdef _MAC_H
  1825.             case MADRsrcType:
  1826.                 ReadPartialResource( MADRsrc, OffSetToSample, curData->data, inOutCount);
  1827.                 OffSetToSample += inOutCount;
  1828.             break;
  1829.             #endif
  1830.             
  1831.             case MADFileType:
  1832.                 if( iRead( inOutCount, (Ptr) curData->data, srcFile)) theErr = MADIncompatibleFile;
  1833.             break;
  1834.             
  1835.             case MADPtrType:
  1836.                 BlockMoveData( MADPtr + OffSetToSample, curData->data, inOutCount);
  1837.                 OffSetToSample += inOutCount;
  1838.             break;
  1839.         }
  1840.         
  1841.         if( curData->amp == 16)
  1842.         {
  1843.             long     ll;
  1844.             short    *shortPtr = (short*) curData->data;
  1845.             
  1846.             for( ll = 0; ll < curData->size/2; ll++) MOT16( &shortPtr[ ll]);
  1847.         }
  1848.     }
  1849. }
  1850.  
  1851. for( i = 0; i < MAXINSTRU; i++) MDriver->fid[ i].firstSample = i * MAXSAMPLE;
  1852. /*********************/
  1853.  
  1854. MDriver->header->MAD = 'MADI';
  1855.  
  1856. //MADCleanDriver( MDriver);
  1857.  
  1858. *music = MDriver;
  1859.  
  1860. return( noErr);
  1861. }
  1862.  
  1863. OSErr MADKillSample( MADMusic *MDriver, short ins, short sample)
  1864. {
  1865.     short    i;
  1866.     Boolean    IsReading;
  1867.     
  1868.     if( MDriver->header == 0L) return -1;
  1869.     
  1870.         IsReading = MDriver->musicUnderModification;
  1871.         MDriver->musicUnderModification = true;
  1872.     
  1873.     if( MDriver->sample[ ins * MAXSAMPLE + sample] == 0L) return -1;
  1874.     if( MDriver->fid[ ins].numSamples <= sample) return -1;
  1875.     
  1876.     if( MDriver->sample[ ins * MAXSAMPLE + sample]->data) DisposePtr( MDriver->sample[ ins * MAXSAMPLE + sample]->data);
  1877.     DisposePtr( (Ptr) MDriver->sample[ ins * MAXSAMPLE + sample]);
  1878.     MDriver->sample[ ins * MAXSAMPLE + sample] = 0L;
  1879.     
  1880.     MDriver->fid[ ins].numSamples--;
  1881.     
  1882.     for( i = sample ; i < MDriver->fid[ ins].numSamples; i++)
  1883.     {
  1884.         MDriver->sample[ ins * MAXSAMPLE + i] = MDriver->sample[ ins * MAXSAMPLE + i + 1];
  1885.     }
  1886.     
  1887.     for( i = 0; i < NUMBER_NOTES; i++)
  1888.     {
  1889.         if( MDriver->fid[ ins].what[ i] >= sample)
  1890.         {
  1891.             if( MDriver->fid[ ins].what[ i] > 0)
  1892.                 MDriver->fid[ ins].what[ i]--;
  1893.         }
  1894.     }
  1895.     
  1896.         MDriver->musicUnderModification = IsReading;
  1897.     
  1898.     return noErr;
  1899. }
  1900.  
  1901. sData    * MADCreateSample( MADMusic *MDriver, short ins, short sample)
  1902. {
  1903.     sData    *curData;
  1904.  
  1905.     curData = (sData*) NewPtrClear( sizeof( sData));
  1906.     if( curData)
  1907.     {
  1908.         curData->size        = 0L;
  1909.         curData->loopBeg    = 0L;
  1910.         curData->loopSize    = 0L;
  1911.         curData->vol        = MAX_VOLUME;
  1912.         curData->c2spd        = NOFINETUNE;
  1913.         curData->loopType    = eClassicLoop;
  1914.         curData->amp        = 8;
  1915.         curData->relNote    = 0;
  1916.         // curData->name
  1917.         curData->data        = 0L;
  1918.         
  1919.         // Install it
  1920.         
  1921.         if( sample < MDriver->fid[ ins].numSamples) MyDebugStr( __LINE__, __FILE__, "");
  1922.         
  1923.         MDriver->sample[ ins * MAXSAMPLE + sample] = curData;
  1924.         MDriver->fid[ ins].numSamples++;
  1925.     }
  1926.     
  1927.     return curData;        
  1928. }
  1929.  
  1930. void MADPurgeTrack( MADDriverRec *intDriver)
  1931. {
  1932.     short    i;
  1933.     
  1934.     for( i=0; i<intDriver->DriverSettings.numChn; i++)
  1935.     {
  1936.     //    intDriver->chan[i].lastWord        = 0;
  1937.     //    intDriver->chan[i].curLevel        = 0;
  1938.         intDriver->chan[i].prevPtr        = 0;
  1939.         
  1940.         intDriver->chan[i].curPtr = intDriver->chan[i].maxPtr;
  1941.         intDriver->chan[i].samplePtr = 0L;
  1942.         intDriver->chan[i].lAC        = 0;
  1943.         intDriver->chan[i].loopBeg = 0;
  1944.         intDriver->chan[i].loopSize = 0;
  1945.         intDriver->chan[i].RemoverWorking = false;
  1946.         intDriver->chan[i].TICKREMOVESIZE = 1;
  1947.         
  1948.         intDriver->chan[i].lastWordR        = 0;
  1949.         intDriver->chan[i].lastWordL        = 0;
  1950.         intDriver->chan[i].curLevelL        = 0;
  1951.         intDriver->chan[i].curLevelR        = 0;
  1952.         intDriver->chan[i].prevPtr        = 0;
  1953.         intDriver->chan[i].prevVol0        = 1;
  1954.         intDriver->chan[i].prevVol1        = 1;
  1955.         intDriver->chan[i].preOff            = 0xFFFFFFFF;
  1956.         intDriver->chan[i].preVal2        = 0;
  1957.         intDriver->chan[i].preVal2R        = 0;
  1958.         
  1959.         intDriver->chan[i].spreVal2        = 0;
  1960.         intDriver->chan[i].spreVal2R    = 0;
  1961.         
  1962.         intDriver->chan[i].preVal            = 0;
  1963.         intDriver->chan[i].spreVal        = 0;
  1964.         intDriver->chan[i].LevelDirectionR = true;
  1965.         intDriver->chan[i].LevelDirectionL = true;
  1966.  
  1967.     }
  1968. }
  1969.  
  1970. void MADPurgeTrackIfInstru( MADDriverRec *intDriver, short instru)
  1971. {
  1972.     short    i;
  1973.     
  1974.     for( i=0; i<intDriver->DriverSettings.numChn; i++)    //
  1975.     {
  1976.         if( intDriver->chan[i].ins == instru && intDriver->chan[i].curPtr != intDriver->chan[i].maxPtr)
  1977.         {
  1978.         //    intDriver->chan[i].lastWord        = 0;
  1979.         //    intDriver->chan[i].curLevel        = 0;
  1980.             intDriver->chan[i].prevPtr        = 0;
  1981.             
  1982.             intDriver->chan[i].curPtr = intDriver->chan[i].maxPtr;
  1983.             intDriver->chan[i].samplePtr = 0L;
  1984.             intDriver->chan[i].lAC        = 0;
  1985.             intDriver->chan[i].loopBeg = 0;
  1986.             intDriver->chan[i].loopSize = 0;
  1987.             intDriver->chan[i].RemoverWorking = false;
  1988.             intDriver->chan[i].TICKREMOVESIZE = 1;
  1989.             
  1990.         intDriver->chan[i].lastWordR        = 0;
  1991.         intDriver->chan[i].lastWordL        = 0;
  1992.         intDriver->chan[i].curLevelL        = 0;
  1993.         intDriver->chan[i].curLevelR        = 0;
  1994.         intDriver->chan[i].prevPtr        = 0;
  1995.         intDriver->chan[i].prevVol0        = 1;
  1996.         intDriver->chan[i].prevVol1        = 1;
  1997.         intDriver->chan[i].preOff            = 0xFFFFFFFF;
  1998.         intDriver->chan[i].preVal2        = 0;
  1999.         intDriver->chan[i].preVal2R        = 0;
  2000.         
  2001.         intDriver->chan[i].spreVal2        = 0;
  2002.         intDriver->chan[i].spreVal2R    = 0;
  2003.         
  2004.         intDriver->chan[i].preVal            = 0;
  2005.         intDriver->chan[i].spreVal        = 0;
  2006.         intDriver->chan[i].LevelDirectionR = true;
  2007.         intDriver->chan[i].LevelDirectionL = true;
  2008.         }
  2009.     }
  2010. }
  2011.  
  2012. OSErr MADCleanCurrentMusic( MADMusic *MDriver, MADDriverRec *intDriver)
  2013. {
  2014.     if( MDriver->header != 0L)
  2015.     {
  2016.         short i, x;
  2017.         
  2018.         if( intDriver)
  2019.         {
  2020.             if( intDriver->Pat > MDriver->header->numPat) MADReset( intDriver);
  2021.             if( intDriver->PartitionReader > MDriver->partition[ intDriver->Pat]->header.size) MADReset( intDriver);
  2022.         }
  2023.         
  2024.         for( i = 0; i < MAXINSTRU; i++)
  2025.         {
  2026.             for( x = 0; x < 96; x++)
  2027.             {
  2028.                 if( MDriver->fid[ i].what[ x])
  2029.                 {
  2030.                     if( MDriver->fid[ i].what[ x] >= MDriver->fid[ i].numSamples)
  2031.                     {
  2032.                         MDriver->fid[ i].what[ x] = 0;
  2033.                     }
  2034.                 }
  2035.             }
  2036.             
  2037.             for( x = 0; x < MDriver->fid[ i].numSamples; x++)
  2038.             {
  2039.                 sData    *curData = MDriver->sample[ i * MAXSAMPLE + x];
  2040.                 
  2041.                 if( curData == 0L)
  2042.                 {
  2043.                     curData = MDriver->sample[ i * MAXSAMPLE + x] = MADCreateSample( MDriver, i, x);
  2044.                 }
  2045.                 
  2046.                 if( curData->data == 0L)
  2047.                 {
  2048.                     curData->data = NewPtr( 0);
  2049.                     curData->size = 0;
  2050.                 }
  2051.                 
  2052.                 if( curData->size <= 0)
  2053.                 {
  2054.                     curData->loopBeg = 0;
  2055.                     curData->loopSize = 0;
  2056.                 }
  2057.                 
  2058.                 if( curData->loopSize < 0) curData->loopSize = 0;
  2059.                 
  2060.                 if( curData->loopBeg > curData->size)
  2061.                 {
  2062.                     curData->loopBeg = 0;
  2063.                     curData->loopSize = 0;
  2064.                 }
  2065.                 
  2066.                 if( curData->loopBeg + curData->loopSize > curData->size)
  2067.                 {
  2068.                     curData->loopBeg = 0;
  2069.                     curData->loopSize = 0;
  2070.                 }
  2071.             }
  2072.         }
  2073.     }
  2074.     
  2075.     return noErr;
  2076. }
  2077.  
  2078. OSErr MADResetInstrument( InstrData        *curIns)
  2079. {
  2080.     short i;
  2081.  
  2082.     for( i = 0; i < 32; i++) curIns->name[ i]    = 0;
  2083.     curIns->type        = 0;
  2084.     curIns->numSamples    = 0;
  2085.     
  2086.     /**/
  2087.     
  2088.     for( i = 0; i < 96; i++) curIns->what[ i]        = 0;
  2089.     for( i = 0; i < 12; i++)
  2090.     {
  2091.         curIns->volEnv[ i].pos        = 0;
  2092.         curIns->volEnv[ i].val        = 0;
  2093.     }
  2094.     for( i = 0; i < 12; i++)
  2095.     {
  2096.         curIns->pannEnv[ i].pos    = 0;
  2097.         curIns->pannEnv[ i].val    = 0;
  2098.     }
  2099.     curIns->volSize        = 0;
  2100.     curIns->pannSize    = 0;
  2101.     
  2102.     curIns->volSus        = 0;
  2103.     curIns->volBeg        = 0;
  2104.     curIns->volEnd        = 0;
  2105.     
  2106.     curIns->pannSus        = 0;
  2107.     curIns->pannBeg        = 0;
  2108.     curIns->pannEnd        = 0;
  2109.     
  2110.     curIns->volType        = 0;
  2111.     curIns->pannType    = 0;
  2112.     
  2113.     curIns->volFade        = DEFAULT_VOLFADE;
  2114.     curIns->vibDepth    = 0;
  2115.     curIns->vibRate        = 0;
  2116.  
  2117.     return noErr;
  2118. }
  2119.  
  2120. OSErr MADKillInstrument( MADMusic *music, short ins)
  2121. {
  2122. short                i;
  2123. InstrData        *curIns;
  2124. Boolean            IsReading;
  2125.  
  2126.     if( music == 0L) return -1;
  2127.     
  2128.     curIns = &music->fid[ ins];
  2129.     
  2130.     IsReading = music->musicUnderModification;
  2131.     music->musicUnderModification = true;
  2132.     
  2133.     for( i = 0; i < curIns->numSamples; i++)
  2134.     {
  2135.         if( music->sample[ ins * MAXSAMPLE + i] != 0L)
  2136.         {
  2137.             if( music->sample[ ins * MAXSAMPLE + i]->data != 0L)
  2138.             {
  2139.                 DisposePtr( (Ptr) music->sample[ ins * MAXSAMPLE + i]->data);
  2140.                 music->sample[ ins * MAXSAMPLE + i]->data = 0L;
  2141.             }
  2142.             DisposePtr( (Ptr) music->sample[ ins * MAXSAMPLE + i]);
  2143.             music->sample[ ins * MAXSAMPLE + i] = 0L;
  2144.         }
  2145.     }
  2146.     
  2147.     MADResetInstrument( curIns);
  2148.     
  2149.     music->musicUnderModification = IsReading;
  2150.     
  2151.     return noErr;
  2152. }
  2153.  
  2154. OSErr MADKillCmd( Cmd    *aCmd)
  2155. {
  2156.     aCmd->cmd     = 0;
  2157.     aCmd->note     = 0xFF;
  2158.     aCmd->arg     = 0;
  2159.     aCmd->ins     = 0;
  2160.     aCmd->vol     = 0xFF;
  2161.  
  2162.     return noErr;
  2163. }
  2164.  
  2165. void MADCheckSpeed( MADMusic *MDriver, MADDriverRec *intDriver)
  2166. {
  2167.     short            i, x, y;
  2168.     short            curPartitionReader;
  2169.     Cmd                *aCmd;
  2170.     Boolean            CmdSpeed = false, FineFound = false, ArgFound[ MAXTRACK], NoteFound[ MAXTRACK], InsFound[ MAXTRACK];
  2171.  
  2172.     if( !MDriver) return;
  2173.     if( MDriver->header == 0L) return;
  2174.     if( !intDriver) return;
  2175.     
  2176.     for( i = 0; i < MAXTRACK; i++)
  2177.     {
  2178.         ArgFound[ i] = false;
  2179.         NoteFound[ i] = false;
  2180.         InsFound[ i] = false;
  2181.     }
  2182.     
  2183.     for( i = intDriver->PL; i >= 0; i--)
  2184.     {
  2185.         if( i == intDriver->PL)
  2186.         {
  2187.             curPartitionReader = intDriver->PartitionReader;
  2188.             if( curPartitionReader >= MDriver->partition[ MDriver->header->oPointers[ i]]->header.size) curPartitionReader--;
  2189.         }
  2190.         else
  2191.         {
  2192.             curPartitionReader = MDriver->partition[ MDriver->header->oPointers[ i]]->header.size-1;
  2193.         }
  2194.  
  2195.         for( x = curPartitionReader; x >= 0; x--)
  2196.         {
  2197.             for( y = MDriver->header->numChn-1; y >= 0 ; y--)
  2198.             {
  2199.                 Channel    *ch = &intDriver->chan[ y];
  2200.                 
  2201.                 aCmd = GetMADCommand( x, y, MDriver->partition[ MDriver->header->oPointers[ i]]);
  2202.                 
  2203.                 if( aCmd->cmd == speedE)
  2204.                 {                    
  2205.                     if( aCmd->arg < 32)
  2206.                     {
  2207.                         if( aCmd->arg != 0)
  2208.                         {
  2209.                             if( CmdSpeed == false) intDriver->speed = aCmd->arg;
  2210.                             CmdSpeed = true;
  2211.                         }
  2212.                     }
  2213.                     else
  2214.                     {
  2215.                         if( aCmd->arg != 0)
  2216.                         {
  2217.                             if( FineFound == false) intDriver->finespeed = aCmd->arg;
  2218.                             FineFound = true;
  2219.                         }
  2220.                     }
  2221.                 }
  2222.                 
  2223.                 if( aCmd->arg != 0)
  2224.                 {
  2225.                     if( !ArgFound[ y]) ch->oldArg[ aCmd->cmd] = aCmd->arg;
  2226.                     ArgFound[ y] = true;
  2227.                 }
  2228.                 
  2229.                 if( aCmd->ins != 0)
  2230.                 {
  2231.                     if( !InsFound[ y]) ch->insOld = aCmd->ins;
  2232.                     InsFound[ y] = true;
  2233.                 }
  2234.                 
  2235.                 if( aCmd->note != 0xFF)
  2236.                 {
  2237.                     if( !NoteFound[ y]) ch->noteOld = aCmd->note;
  2238.                     NoteFound[ y] = true;
  2239.                 }
  2240.             }
  2241.         }
  2242.     }
  2243.         
  2244.  
  2245.     if( !CmdSpeed)     intDriver->speed         = MDriver->header->speed;
  2246.     if( !FineFound) intDriver->finespeed     = MDriver->header->tempo;
  2247. }
  2248.  
  2249. void MADCheckSpeedPattern( MADMusic *MDriver, MADDriverRec *intDriver)
  2250. {
  2251.     short            x, y;
  2252.     short            curPartitionReader;
  2253.     Cmd                *aCmd;
  2254.     Boolean            CmdSpeed = false, FineFound = false;
  2255.  
  2256.     if( !MDriver) return;
  2257.     if( MDriver->header == 0L) return;
  2258.     if( !intDriver) return;
  2259.     
  2260.  
  2261.     curPartitionReader = intDriver->PartitionReader;
  2262.     if( curPartitionReader >= MDriver->partition[ intDriver->Pat]->header.size) curPartitionReader--;
  2263.     
  2264.     for( x = curPartitionReader; x >= 0; x--)
  2265.     {
  2266.         for( y = MDriver->header->numChn-1; y >= 0 ; y--)
  2267.         {
  2268.             Channel    *ch = &intDriver->chan[ y];
  2269.             
  2270.             aCmd = GetMADCommand( x, y, MDriver->partition[ intDriver->Pat]);
  2271.             
  2272.             if( aCmd->cmd == speedE)
  2273.             {                    
  2274.                 if( aCmd->arg < 32)
  2275.                 {
  2276.                     if( aCmd->arg != 0)
  2277.                     {
  2278.                         if( CmdSpeed == false) intDriver->speed = aCmd->arg;
  2279.                         CmdSpeed = true;
  2280.                     }
  2281.                 }
  2282.                 else
  2283.                 {
  2284.                     if( aCmd->arg != 0)
  2285.                     {
  2286.                         if( FineFound == false) intDriver->finespeed = aCmd->arg;
  2287.                         FineFound = true;
  2288.                     }
  2289.                 }
  2290.             }
  2291.         }
  2292.     }
  2293.  
  2294.     if( !CmdSpeed)     intDriver->speed         = MDriver->header->speed;
  2295.     if( !FineFound) intDriver->finespeed     = MDriver->header->tempo;
  2296. }
  2297.  
  2298. OSErr MADPlayMusic( MADDriverRec *MDriver)
  2299. {
  2300.     if( !MDriver) return MADParametersErr;
  2301.     if( !MDriver->curMusic) return MADDriverHasNoMusic;
  2302.     
  2303.     MDriver->Reading = true;
  2304.     
  2305.     return noErr;
  2306. }
  2307.  
  2308. OSErr MADStopMusic( MADDriverRec *MDriver)
  2309. {
  2310.     if( !MDriver) return MADParametersErr;
  2311.     if( !MDriver->curMusic) return MADDriverHasNoMusic;
  2312.     
  2313.     MDriver->Reading = false;
  2314.     #ifdef _MIDIHARDWARE_
  2315.     if( MDriver->SendMIDIClockData) SendMIDIClock( MDriver, 0xFC);
  2316.     #endif
  2317.     
  2318.     return noErr;
  2319. }
  2320.  
  2321. OSErr MADStartDriver( MADDriverRec *MDriver)
  2322. {
  2323.     MDriver->MADPlay = true;
  2324.     MDriver->Reading = false;
  2325.     MDriver->musicEnd = false;
  2326.     
  2327.     MADCleanDriver( MDriver);
  2328.     
  2329.     MADCheckSpeed( MDriver->curMusic, MDriver);
  2330.     
  2331.     switch( MDriver->DriverSettings.driverMode)
  2332.     {
  2333.         #ifdef _MAC_H
  2334. /*    case AWACSoundDriver:
  2335.         if( (*(unsigned char *)AMICCodecStatus2 & kHeadphonesInserted))
  2336.         {
  2337.             curR1 = 0;
  2338.             curR1 |= kSpeakerMuteBit;
  2339.             WRITE_AWACS_REG_1;
  2340.         }
  2341.  
  2342.         
  2343.         CLEAR_SND_OUT_BUF_INTS;
  2344.         CLEAR_UNDERRUN_INT;
  2345.         ENABLE_BUF_AND_UNDERRUN_INTS;
  2346.         SND_OUT_DMA_ENABLE;
  2347.     break;
  2348.     
  2349.     case ASCSoundDriver:*/
  2350.     case MIDISoundDriver:
  2351.         PlayChannel( MDriver);
  2352.     break;
  2353.     
  2354.     case SoundManagerDriver:
  2355.         PlayChannel( MDriver);
  2356.     break;
  2357.     #endif
  2358.     }
  2359.     
  2360.     return( noErr);
  2361. }
  2362.  
  2363. OSErr MADStopDriver( MADDriverRec *MDriver)
  2364. {
  2365.     MDriver->MADPlay = false;
  2366.  
  2367.     switch( MDriver->DriverSettings.driverMode)
  2368.     {
  2369.     #ifdef _MAC_H
  2370. /*    case AWACSoundDriver:
  2371.         DISABLE_BUF_AND_UNDERRUN_INTS;
  2372.         SND_OUT_DMA_DISABLE;
  2373.         CLEAR_UNDERRUN_INT;
  2374.     break;
  2375.     
  2376.     case ASCSoundDriver:*/
  2377.     case MIDISoundDriver:
  2378.         AllNoteOff( MDriver);
  2379.         StopChannel( MDriver);
  2380.     break;
  2381.     
  2382.     case SoundManagerDriver:
  2383.         StopChannel( MDriver);
  2384.     break;
  2385.     #endif
  2386.     }
  2387.     
  2388.     MADCleanDriver( MDriver);
  2389.     
  2390.     #ifdef _MIDIHARDWARE_
  2391.     if( MDriver->SendMIDIClockData) SendMIDIClock( MDriver, 0xFC);
  2392.     #endif
  2393.     
  2394.     return( noErr);
  2395. }
  2396.  
  2397. OSErr MADReset( MADDriverRec *MDriver)
  2398. {
  2399.     MADCleanDriver( MDriver);
  2400.     
  2401.     MDriver->BufCounter = 0;
  2402.     MDriver->BytesToGenerate = 0;
  2403.     MDriver->smallcounter = 128;
  2404.     
  2405.     MDriver->PL = 0;
  2406.     MDriver->PartitionReader = 0;
  2407.     
  2408.     if( MDriver->curMusic != 0L)
  2409.     {
  2410.         MDriver->Pat = MDriver->curMusic->header->oPointers[ MDriver->PL];
  2411.         MDriver->speed = MDriver->curMusic->header->speed;
  2412.         MDriver->finespeed = MDriver->curMusic->header->tempo;
  2413.     }
  2414.     
  2415.     return( noErr);
  2416. }
  2417.  
  2418. short MADGetNextReader( MADMusic *music, MADDriverRec *intDriver, short cur, short *pat)
  2419. {
  2420.     if( music == 0L) return 0;
  2421.     if( intDriver == 0L) return 0;
  2422.     if( music->header == 0L) return 0;
  2423.     
  2424.     cur++;
  2425.     if( cur >= music->partition[ intDriver->Pat]->header.size)
  2426.     {
  2427.         cur = 0;
  2428.         
  2429.         if( intDriver->JumpToNextPattern)
  2430.         {
  2431.             *pat = music->header->oPointers[ intDriver->PL + 1];
  2432.             
  2433.             if( intDriver->speed == 1 && intDriver->PL + 1 >= music->header->numPointers)
  2434.             {
  2435.                 *pat = music->header->oPointers[ 0];
  2436.             }
  2437.         }
  2438.     }
  2439.     
  2440.     return cur;
  2441. }
  2442.  
  2443. OSErr MADDisposeMusic( MADMusic **music, MADDriverRec *MDriver)
  2444. {
  2445.     short        x;
  2446.     
  2447.     if( !music) return noErr;
  2448.     if( !*music) return noErr;
  2449.     
  2450. //    if( (*music)->currentDriver != 0L)
  2451.     {
  2452.         (*music)->musicUnderModification = true;
  2453.     //    MADPurgeTrack(  (*music)->currentDriver);
  2454.     //    MADCleanDriver(  (*music)->currentDriver);
  2455.         
  2456.     //    MADDetachMusicFromDriver( *music);
  2457.     }
  2458.     
  2459.     if(  (*music)->header != 0L)
  2460.     {
  2461.         for( x = 0; x < (*music)->header->numPat ; x++)
  2462.         {
  2463.             if(  (*music)->partition[ x] != 0L) DisposePtr( (Ptr)  (*music)->partition[ x]);
  2464.              (*music)->partition[ x] = 0L;
  2465.         }
  2466.         
  2467.         for( x = (*music)->header->numPat; x < MAXPATTERN ; x++)
  2468.         {
  2469.             if(  (*music)->partition[ x] != 0L) DebugStr("\pMADDispose (*music) !");
  2470.         }
  2471.         
  2472.         for( x = 0; x < MAXINSTRU ; x++)
  2473.         {
  2474.             MADKillInstrument( *music, x);
  2475.         }
  2476.         
  2477.         DisposePtr( (Ptr) (*music)->header);
  2478.         
  2479.         if(  (*music)->fid == 0L) DebugStr("\pMADDispose (*music) !");
  2480.         DisposePtr( (Ptr) (*music)->fid);
  2481.         
  2482.         if(  (*music)->sample == 0L) DebugStr("\pMADDispose (*music) !");
  2483.         DisposePtr( (Ptr) (*music)->sample);
  2484.     }
  2485.     
  2486.     if( MDriver)
  2487.     {
  2488.         if( MDriver->curMusic == *music) MDriver->curMusic = 0L;
  2489.     }
  2490.     
  2491.     DisposePtr( (Ptr) *music);
  2492.      *music = 0L;
  2493.     
  2494.     return( noErr);
  2495. }
  2496.  
  2497. #pragma options align=mac68k
  2498.  
  2499. Boolean    MADIsPressed( unsigned char* km2, unsigned short k)
  2500. {
  2501.     return( (Boolean) ((km2[k>>3] >> (k & 7)) & 1) );
  2502. }
  2503.  
  2504. #ifdef _MAC_H
  2505. OSErr MADPlaySoundDataSYNC( MADDriverRec *MDriver, Ptr soundPtr, long size, long channel, long note, long amplitude, long loopBeg, long loopSize, unsigned long rate, Boolean stereo)
  2506. {
  2507.     OSErr        iErr;
  2508.     Boolean        continueLoop;
  2509.     KeyMap        km;
  2510.     
  2511.     iErr = MADPlaySoundData( MDriver, soundPtr, size, channel, note, amplitude, 0, 0, rate, stereo);
  2512.     if( iErr == noErr)
  2513.     {
  2514.         continueLoop = true;
  2515.         while( continueLoop)
  2516.         {
  2517.             GetKeys( km);
  2518.             
  2519.             if( MDriver->chan[ channel].samplePtr == 0L) continueLoop = false;
  2520.             if( MADIsPressed( (unsigned char*) km, 0x37) && MADIsPressed( (unsigned char*) km, 0x2F)) continueLoop = false;
  2521.             if( Button()) continueLoop = false;
  2522.         }
  2523.         
  2524.         if( MDriver->chan[ channel].samplePtr != 0L)
  2525.         {
  2526.             MDriver->chan[ channel].curPtr         = MDriver->chan[ channel].maxPtr;
  2527.             MDriver->chan[ channel].samplePtr    = 0L;
  2528.             MDriver->chan[ channel].lAC            = 0;
  2529.             MDriver->chan[ channel].loopBeg     = 0;
  2530.             MDriver->chan[ channel].loopSize     = 0;
  2531.         }
  2532.     }
  2533.     
  2534.     return iErr;
  2535. }
  2536. #endif
  2537.  
  2538. OSErr MADPlaySoundData( MADDriverRec *MDriver, Ptr soundPtr, long size, long channel, long note, long amplitude, long loopBeg, long loopSize, unsigned long rate, Boolean stereo)
  2539. {
  2540.     Channel *curVoice;
  2541.     
  2542.     if( channel < 0)                                    return MADParametersErr;
  2543.     if( channel >= MDriver->DriverSettings.numChn)    return MADParametersErr;
  2544.     
  2545.     if( MDriver->curMusic != 0L)
  2546.     {
  2547.         MDriver->curMusic->musicUnderModification = true;
  2548.     }
  2549.     
  2550.     curVoice = &MDriver->chan[ channel];
  2551.     
  2552.     curVoice->prevPtr        = 0L;
  2553.     curVoice->samplePtr        = soundPtr;
  2554.     curVoice->stereo            = stereo;
  2555.     
  2556.     curVoice->maxPtr     = curVoice->curPtr = curVoice->begPtr = soundPtr;
  2557.     curVoice->maxPtr     += size;
  2558.     curVoice->sizePtr    = size;
  2559.     curVoice->lAC        = 0;
  2560.     curVoice->pingpong    = false;
  2561.     curVoice->PanningE8    = false;
  2562.     curVoice->trig            = 0;
  2563.     curVoice->preOff        = 0xFFFFFFFF;
  2564.     curVoice->preVal        = 0;
  2565.     curVoice->spreVal        = 0;
  2566.     curVoice->preVal2        = *curVoice->curPtr;
  2567.     if( curVoice->amp == 8) curVoice->preVal2R    = *(curVoice->curPtr+1);
  2568.     else curVoice->preVal2R    = *(curVoice->curPtr+2);
  2569.     curVoice->spreVal2    = *(short*) curVoice->curPtr;
  2570.     curVoice->spreVal2R    = *(short*) (curVoice->curPtr+2);
  2571.     
  2572.     if( note == 0xFF) note = 48;
  2573.     curVoice->note        = note;
  2574.     curVoice->ins        = 0;
  2575.     curVoice->viboffset    = 0;
  2576.     curVoice->amp        = amplitude;
  2577.     curVoice->fineTune    = rate >> 16L;
  2578.     
  2579.     curVoice->period    = GetOldPeriod( curVoice->note, curVoice->fineTune, MDriver);
  2580.     
  2581.     if( loopBeg > size)                     { loopBeg = 0; loopSize = 0;}
  2582.     if( loopBeg + loopSize > size)             { loopBeg = 0; loopSize = 0;}
  2583.     
  2584.     curVoice->loopBeg         = loopBeg;
  2585.     curVoice->loopSize         = loopSize;
  2586.     
  2587.     if( loopSize > 0)
  2588.     {
  2589.         curVoice->maxPtr     = (Ptr) ((long) curVoice->begPtr + loopBeg + loopSize);
  2590.     }
  2591.     
  2592.     curVoice->pann        = 32;
  2593.     
  2594.     curVoice->vol             = 64;
  2595.     curVoice->volFade        = 32767;
  2596.     curVoice->volEnv        = 64;
  2597.     curVoice->KeyOn            = true;
  2598.     
  2599.     if( MDriver->curMusic != 0L)
  2600.     {
  2601.         MDriver->curMusic->musicUnderModification = false;
  2602.     }
  2603.     
  2604.     return noErr;
  2605. }
  2606.  
  2607. void MADKeyOFF( MADDriverRec *MDriver, short track)
  2608. {
  2609.     if( track == -1)
  2610.     {
  2611.         short i;
  2612.         
  2613.         for( i = 0; i < MAXTRACK; i++) MDriver->chan[ i].KeyOn = false;
  2614.     }
  2615.     else MDriver->chan[ track].KeyOn = false;
  2616. }
  2617.  
  2618. #ifdef _MAC_H
  2619. OSErr MADPlaySndHandle( MADDriverRec *MDriver, Handle sound, long channel, long note)
  2620. {
  2621.     Ptr             soundPtr;
  2622.     short             soundFormat;
  2623.     short             numSynths, numCmds;
  2624.     long             offset;
  2625.     SoundHeaderPtr     header;
  2626.     CmpSoundHeader    *CmpHeader;
  2627.     ExtSoundHeader    *ExtHeader;
  2628.     long            i;
  2629.     Boolean            stereo = false;
  2630.     Channel            *curVoice;
  2631.     OSType            Scomp;
  2632.     long            Ssize, Samp, SbaseFreq, Sc2spd, Sstart, Send;
  2633.     Ptr                rPtr;
  2634.     
  2635.     /* make the sound safe to use at interrupt time. */
  2636.     HLock( sound);
  2637.     
  2638.     soundPtr = *sound;
  2639.     
  2640.     /* determine what format sound we have. */
  2641.     soundFormat = *(short*)soundPtr;
  2642.     
  2643.     switch(soundFormat)
  2644.     {
  2645.         case 1:                        /* format 1 sound. */
  2646.             /* look inside the format 1 resource and deduce offsets. */
  2647.             numSynths = ((short*)soundPtr)[1];                    /* get # synths. */
  2648.             numCmds = *(short*)(soundPtr+4+numSynths*6);        /* get # commands. */
  2649.         break;
  2650.         
  2651.         case 2:                        /* format 2 sound. */
  2652.             numSynths = 0;            /* format 2 sounds have no synth's. */
  2653.             numCmds = ((short*)soundPtr)[2];
  2654.         break;
  2655.         
  2656.         default:                    /* jack says, what about 12? or 6? */
  2657.             HUnlock( sound);
  2658.             return MADUnknowErr;
  2659.         break;
  2660.     } 
  2661.  
  2662.     /* compute address of sound header. */
  2663.     offset = 6 + 6*numSynths + 8*numCmds;
  2664.     header = (SoundHeaderPtr) (StripAddress(*sound) + offset);
  2665.     
  2666.     switch( header->encode)
  2667.     {
  2668.         case cmpSH:
  2669.             CmpHeader = (CmpSoundHeader*) header;
  2670.             if( CmpHeader->numChannels > 2)
  2671.             {
  2672.                 HUnlock( sound);
  2673.                 return MADUnknowErr;
  2674.             }
  2675.             
  2676.             if( CmpHeader->numChannels == 2) stereo = true;
  2677.             else stereo = false;
  2678.             
  2679.             Sstart        = CmpHeader->loopStart;
  2680.             Send        = CmpHeader->loopEnd;
  2681.             Samp        = CmpHeader->sampleSize;
  2682.             Sc2spd        = CmpHeader->sampleRate;
  2683.             SbaseFreq     = CmpHeader->baseFrequency;
  2684.             Ssize         = CmpHeader->numFrames;
  2685.             rPtr         = (Ptr) CmpHeader->sampleArea;
  2686.             Scomp        = CmpHeader->compressionID;
  2687.         break;
  2688.  
  2689.         case extSH:
  2690.             ExtHeader    = (ExtSoundHeader*) header;
  2691.             if( ExtHeader->numChannels > 2)
  2692.             {
  2693.                 HUnlock( sound);
  2694.                 return MADUnknowErr;
  2695.             }
  2696.             
  2697.             if( ExtHeader->numChannels == 2) stereo = true;
  2698.             else stereo = false;
  2699.             
  2700.             Sstart        = ExtHeader->loopStart;
  2701.             Send        = ExtHeader->loopEnd;
  2702.             Samp        = ExtHeader->sampleSize;
  2703.             Sc2spd        = ExtHeader->sampleRate;
  2704.             SbaseFreq     = ExtHeader->baseFrequency;
  2705.             Ssize         = ExtHeader->numFrames;
  2706.             rPtr         = (Ptr) ExtHeader->sampleArea;
  2707.             Scomp        = 'NONE';
  2708.         break;
  2709.         
  2710.         default:
  2711.         case stdSH:
  2712.             Sstart        = header->loopStart;
  2713.             Send        = header->loopEnd;
  2714.             if( header->encode == 0x40) Samp        = 0;
  2715.             else Samp        = 8;
  2716.             Sc2spd        = header->sampleRate;
  2717.             SbaseFreq     = header->baseFrequency;
  2718.             Ssize         = header->length;
  2719.             rPtr         = (Ptr) header->sampleArea;
  2720.             Scomp        = 'NONE';
  2721.         break;
  2722.     }
  2723.     
  2724.     if( Samp == 16)
  2725.     {
  2726.         Ssize     *= 2L;
  2727.         Sstart     *= 2L;
  2728.         Send     *= 2L;
  2729.         
  2730.         if( stereo)
  2731.         {
  2732.             Ssize     *= 2L;
  2733.             Sstart     *= 2L;
  2734.             Send     *= 2L;
  2735.         }
  2736.     }
  2737.     else
  2738.     {
  2739.         if( Samp == 8)
  2740.         {
  2741.             if( stereo)
  2742.             {
  2743.                 Ssize     *= 2L;
  2744.                 Sstart     *= 2L;
  2745.                 Send     *= 2L;
  2746.             }
  2747.             
  2748.             for( i = 0; i < Ssize; i++) rPtr[ i] = rPtr[ i] - 0x80;
  2749.             
  2750.             switch( header->encode)
  2751.             {
  2752.                 case extSH:
  2753.                     ExtHeader->sampleSize = 0;
  2754.                 break;
  2755.                 
  2756.                 case cmpSH:
  2757.                     CmpHeader->sampleSize = 0;
  2758.                 break;
  2759.                 
  2760.                 default:
  2761.                 case stdSH:
  2762.                     header->encode = 0x40;
  2763.                 break;
  2764.             }
  2765.         }
  2766.         Samp = 8;
  2767.     }
  2768.     
  2769.     Sc2spd = Sc2spd >> 16L;
  2770.     
  2771.     /*********************/
  2772.     if( channel < 0)                                    return MADParametersErr;
  2773.     if( channel >= MDriver->DriverSettings.numChn)        return MADParametersErr;
  2774.     
  2775.     if( MDriver->curMusic != 0L)
  2776.     {
  2777.         MDriver->curMusic->musicUnderModification = true;
  2778.     }
  2779.     
  2780.     curVoice = &MDriver->chan[ channel];
  2781.  
  2782.     
  2783.     if( note == 0xFF) note = 48;
  2784.     
  2785.     curVoice->prevPtr        = 0L;
  2786.     curVoice->samplePtr        = rPtr;
  2787.     curVoice->stereo            = stereo;
  2788.     
  2789.     curVoice->note        = note + (60 - SbaseFreq);
  2790.     curVoice->period    = GetOldPeriod( curVoice->note, Sc2spd, MDriver);
  2791.     
  2792.     curVoice->ins        = 0;
  2793.     curVoice->viboffset    = 0;
  2794.     curVoice->amp        = Samp;
  2795.     curVoice->fineTune    = Sc2spd;
  2796.  
  2797.     curVoice->maxPtr     = curVoice->curPtr = curVoice->begPtr = rPtr;
  2798.     curVoice->maxPtr     += Ssize;
  2799.     curVoice->sizePtr    = Ssize;
  2800.     curVoice->lAC        = 0;
  2801.     curVoice->pingpong    = false;
  2802.     curVoice->PanningE8    = false;
  2803.     curVoice->trig        = 0;
  2804.     curVoice->preOff        = 0xFFFFFFFF;
  2805.     curVoice->preVal        = 0;
  2806.     curVoice->spreVal        = 0;
  2807.     curVoice->preVal2        = *curVoice->curPtr;
  2808.     if( curVoice->amp == 8) curVoice->preVal2R    = *(curVoice->curPtr+1);
  2809.     else curVoice->preVal2R    = *(curVoice->curPtr+2);
  2810.     curVoice->spreVal2    = *(short*) curVoice->curPtr;
  2811.     curVoice->spreVal2R    = *(short*) (curVoice->curPtr+2);
  2812.     
  2813.     if( Send == Sstart || Send - Sstart < 3)
  2814.     {
  2815.         Send = 0;
  2816.         Sstart = 0;
  2817.     }
  2818.     
  2819.     curVoice->loopBeg         = Sstart;
  2820.     curVoice->loopSize         = Send - Sstart;
  2821.     
  2822.     if( curVoice->loopSize > 0)
  2823.     {
  2824.         curVoice->maxPtr     = (Ptr) ((long) curVoice->begPtr + Send);
  2825.     }
  2826.     
  2827.     curVoice->pann        = 32;
  2828.     curVoice->vol         = 64;
  2829.     curVoice->volFade        = 32767;
  2830.     curVoice->volEnv        = 64;
  2831.     curVoice->KeyOn        = true;
  2832.     
  2833.     if( MDriver->curMusic != 0L)
  2834.     {
  2835.         MDriver->curMusic->musicUnderModification = false;
  2836.     }
  2837.     
  2838.     return noErr;
  2839. }
  2840.  
  2841. #endif
  2842.  
  2843. #pragma options align=reset
  2844.  
  2845. Cmd* GetMADCommand( short PosX, short    TrackIdX, PatData*    tempMusicPat)
  2846. {
  2847.     if( PosX < 0) PosX = 0;
  2848.     else if( PosX >= tempMusicPat->header.size) PosX = tempMusicPat->header.size -1;
  2849.     
  2850.     return( &(tempMusicPat->Cmds[ (tempMusicPat->header.size * TrackIdX) + PosX]));
  2851. }
  2852.  
  2853. void MADDisposeVolumeTable( MADDriverRec *intDriver)
  2854. {
  2855.     if( intDriver->DriverSettings.outPutMode == DeluxeStereoOutPut) MADKillOverShoot( intDriver);
  2856. }
  2857.  
  2858. OSErr MADCreateVolumeTable( MADDriverRec *intDriver)
  2859. {
  2860. long            Tracks = intDriver->DriverSettings.numChn;
  2861. OSErr            theErr;
  2862.  
  2863.     theErr = MADCreateMicroDelay( intDriver);            if( theErr != noErr) return theErr;
  2864.  
  2865.     switch( intDriver->DriverSettings.outPutMode)
  2866.     {
  2867.         case DeluxeStereoOutPut:            Tracks    = 1;        MADCreateOverShoot( intDriver);    break;
  2868.         case PolyPhonic:                    Tracks     = 1;        break;
  2869.     }
  2870.     
  2871.     return( noErr);
  2872. }
  2873.  
  2874. void MADChangeTracks( MADDriverRec *MDriver, short newVal)
  2875. {
  2876.     Boolean    play = MDriver->MADPlay, reading = MDriver->Reading;
  2877.     
  2878.     MADStopDriver( MDriver);
  2879.     
  2880.     MDriver->DriverSettings.numChn = newVal;
  2881.     
  2882.     MADDisposeVolumeTable( MDriver);
  2883.     MADCreateVolumeTable( MDriver);
  2884.     
  2885.     MDriver->trackDiv = MDriver->DriverSettings.numChn;
  2886.     
  2887.     if( play) MADStartDriver( MDriver);
  2888.     if( reading) MADPlayMusic( MDriver);
  2889. }
  2890.  
  2891. void UpdateTracksNumber( MADDriverRec *MDriver)
  2892. {
  2893.     if( MDriver->curMusic != 0L)
  2894.     {
  2895.         if( MDriver->curMusic->header != 0L)
  2896.         {
  2897.             if( MDriver->curMusic->header->numChn != MDriver->DriverSettings.numChn)
  2898.             {
  2899.                 MADChangeTracks( MDriver, MDriver->curMusic->header->numChn);
  2900.             }
  2901.         }
  2902.     }
  2903. }
  2904.  
  2905. OSErr MADCreateVibrato( MADDriverRec *MDriver)
  2906. {
  2907. short            i, vibrato_table[ 64] = 
  2908.  
  2909. {
  2910.     0,24,49,74,97,120,141,161,
  2911.     180,197,212,224,235,244,250,253,
  2912.     255,253,250,244,235,224,212,197,
  2913.     180,161,141,120,97,74,49,24
  2914. };
  2915.  
  2916. /*    {
  2917.         0,50,100,149,196,241,284,325,362,396,426,452,473,490,502,510,512,
  2918.         510,502,490,473,452,426,396,362,325,284,241,196,149,100,50,0,-49,
  2919.         -99,-148,-195,-240,-283,-324,-361,-395,-425,-451,-472,-489,-501,
  2920.         -509,-511,-509,-501,-489,-472,-451,-425,-395,-361,-324,-283,-240,
  2921.         -195,-148,-99,-49
  2922.     };*/
  2923.     
  2924.     
  2925.  
  2926.     for( i = 0; i < 64; i++) MDriver->vibrato_table[ i] = vibrato_table[ i];
  2927.  
  2928.     return noErr;
  2929.  
  2930. /*
  2931. double            base, pitch;
  2932. short            i, j, k;
  2933.  
  2934.     for (j = -8; j < 8; j++)
  2935.     {
  2936.         k = j < 0 ? j + 16 : j;
  2937.         base = AMIGA_CLOCKFREQ2 / 440.0 / 4.0 / pow(2.0, j/96.0);
  2938.     
  2939.         for (i = 0; i < NUMBER_NOTES + 4; i++)
  2940.         {
  2941.                 pitch = base / pow(2.0, (i+1) /12.0);
  2942.                 pitchTable[ i][ k] = floor(pitch + 0.5);
  2943.         }
  2944.     }
  2945. */
  2946. }
  2947. /*
  2948. #ifdef _MAC_H
  2949.  
  2950. #define VIA 0x1D4
  2951.  
  2952. #if defined(powerc) || defined (__powerc) || defined( NO_ASM)
  2953. void VIAOn2()
  2954. {
  2955.     register    Ptr        VIAPtr = *(Ptr*) VIA, ASCBasePtr = *(Ptr*) ASCBase;
  2956.  
  2957.     BitSet( (Ptr) VIAPtr, 0);
  2958.  
  2959.     if( ASCBasePtr[ 0x801] == 0)
  2960.     {
  2961.         ASCBasePtr[ 0x801] = 0x01;
  2962.         ASCBasePtr[ 0x806] = 0xFF;
  2963.     }
  2964.     
  2965.     BitSet( &ASCBasePtr[ 0x802], 6);
  2966. }
  2967.  
  2968. void SndOff()
  2969. {
  2970.     Ptr        VIAPtr = *(Ptr*) VIA;
  2971.  
  2972.     BitSet( (Ptr) VIAPtr, 0);
  2973.     *((Byte*) SoundActive) = 0x05;
  2974. }
  2975.  
  2976. void SndOn()
  2977. {
  2978.     Ptr        VIAPtr = *(Ptr*) VIA;
  2979.  
  2980.     if( BitTst( (Ptr) VIAPtr, 0))
  2981.     {
  2982.         BitClr( (Ptr) VIAPtr, 0);
  2983.          *((Byte*) SoundActive) = 0x05;
  2984.     }
  2985. }
  2986. #else
  2987.  
  2988. void VIAOn2()
  2989. {
  2990. asm
  2991. {
  2992.         MOVEA.L VIA,A0
  2993.         BSET    #7,(A0)
  2994.         
  2995.         MOVE.L    ASCBase, A0
  2996.         TST.B    0x801(A0)
  2997.         BNE.S   @lcf_2
  2998.         MOVE.B    #01,     0x801(A0)
  2999.         MOVE.B    #0xFF,     0x806(A0)
  3000. @lcf_2    BSET    #1,        0x802(A0)
  3001. }
  3002. }
  3003.  
  3004. void SndOff()
  3005. {
  3006. asm
  3007. {
  3008.     MOVEA.L    VIA,A0
  3009.     BSET    #7,(A0)
  3010.     MOVE.B    #0x05, SoundActive
  3011. }
  3012. }
  3013.  
  3014. void SndOn()
  3015. {
  3016. asm
  3017. {        MOVEA.L VIA,A0
  3018.          BTST    #7,(A0)
  3019.          BEQ.S   @lcf_1
  3020.          BCLR    #7,(A0)
  3021.          MOVE.B    #0x05, SoundActive
  3022. @lcf_1    
  3023. }
  3024. }
  3025. #endif
  3026. #endif*/
  3027.  
  3028. enum
  3029. {
  3030.     ins     = 1,
  3031.     note    = 2,
  3032.     cmd        = 4,
  3033.     argu    = 8,
  3034.     vol        = 16
  3035. };
  3036.  
  3037. PatData* DecompressPartitionMAD1( MADMusic *MDriver, PatData* myPat)
  3038. {
  3039.     PatData*                finalPtr;
  3040.     Byte                     *srcPtr;
  3041.     Cmd                        *myCmd;
  3042.     short                    maxCmd;
  3043.     Byte                    set;
  3044.     
  3045.     finalPtr = ( PatData*) NewPtr( sizeof( PatHeader) + myPat->header.size * MDriver->header->numChn * sizeof( Cmd));
  3046.     if( finalPtr == 0L) return 0L;
  3047.     
  3048.     BlockMoveData( myPat, finalPtr, sizeof( PatHeader));
  3049.  
  3050.     srcPtr = (Byte*) myPat->Cmds;
  3051.     myCmd = (Cmd*) finalPtr->Cmds;
  3052.     maxCmd = finalPtr->header.size * MDriver->header->numChn;
  3053.     
  3054.     /*** Decompression Routine ***/
  3055.     
  3056.     while( maxCmd != 0)
  3057.     {
  3058.         maxCmd--;
  3059.         
  3060.         MADKillCmd( myCmd);
  3061.         
  3062.         set = *srcPtr++;
  3063.         
  3064.         if( set & ins)    myCmd->ins = *srcPtr++;
  3065.         if( set & note)    myCmd->note = *srcPtr++;
  3066.         if( set & cmd)    myCmd->cmd = *srcPtr++;
  3067.         if( set & argu)    myCmd->arg = *srcPtr++;
  3068.         if( set & vol)    myCmd->vol = *srcPtr++;
  3069.         
  3070.         myCmd++;
  3071.     }
  3072.     
  3073.     return finalPtr;
  3074. }
  3075.  
  3076. PatData* CompressPartitionMAD1( MADMusic *MDriver, PatData* myPat)
  3077. {
  3078.     PatData*                finalPtr;
  3079.     Byte                     *dstPtr, *setByte;
  3080.     Cmd                        *myCmd;
  3081.     long                    maxCmd;
  3082.     long                    NewPtrSize;
  3083.     Byte                    set;
  3084.     
  3085.     finalPtr = ( PatData*) NewPtr( sizeof( PatHeader) + myPat->header.size * MDriver->header->numChn * 6L);
  3086.     if( finalPtr == 0L) DebugStr("\pCompressPartitionMAD1");
  3087.     
  3088.     BlockMoveData( myPat, finalPtr, sizeof( PatHeader));
  3089.     
  3090.     dstPtr = (Byte*) finalPtr->Cmds;
  3091.     myCmd = (Cmd*) myPat->Cmds;
  3092.     maxCmd = myPat->header.size * MDriver->header->numChn;
  3093.     
  3094.     /*** Compression Routine ***/
  3095.     
  3096.     set         = 0;
  3097.     NewPtrSize     = 0;
  3098.     
  3099.     while( maxCmd != 0)
  3100.     {
  3101.         maxCmd--;
  3102.         
  3103.         // Set byte
  3104.         set            = 0;
  3105.         setByte     = dstPtr;
  3106.         *dstPtr++     = 0x00;
  3107.         NewPtrSize++;
  3108.         ////////
  3109.         
  3110.         if( myCmd->ins > 0)
  3111.         {
  3112.             set += ins;
  3113.             *dstPtr++ = myCmd->ins;
  3114.             NewPtrSize++;
  3115.         }
  3116.         
  3117.         if( myCmd->note != 0xFF)
  3118.         {
  3119.             set += note;
  3120.             *dstPtr++ = myCmd->note;
  3121.             NewPtrSize++;
  3122.         }
  3123.  
  3124.         if( myCmd->cmd > 0)
  3125.         {
  3126.             set += cmd;
  3127.             *dstPtr++ = myCmd->cmd;
  3128.             NewPtrSize++;
  3129.         }
  3130.  
  3131.         if( myCmd->arg > 0)
  3132.         {
  3133.             set += argu;
  3134.             *dstPtr++ = myCmd->arg;
  3135.             NewPtrSize++;
  3136.         }
  3137.  
  3138.         if( myCmd->vol != 0xFF)
  3139.         {
  3140.             set += vol;
  3141.             *dstPtr++ = myCmd->vol;
  3142.             NewPtrSize++;
  3143.         }
  3144.         // Set byte
  3145.         *setByte = set;
  3146.         ////////
  3147.         
  3148.         myCmd++;
  3149.     }
  3150.     
  3151.     finalPtr->header.patBytes = NewPtrSize;
  3152.     
  3153. //    SetPtrSize( (Ptr) finalPtr, (long)(NewPtrSize + sizeof( PatHeader)));
  3154.     
  3155.     return finalPtr;
  3156. }
  3157.  
  3158. /*
  3159. #ifdef _MAC_H
  3160. void DMAIntInstall( unsigned long refCon, Ptr handlerAddr, unsigned long vectorBit)
  3161. {
  3162.     Ptr        ExpandPtr = (Ptr) 0x02B6L;
  3163.     Ptr        dstPtr;
  3164.     
  3165.     dstPtr = *((Ptr*) ExpandPtr);
  3166.     dstPtr += 0x0210;
  3167.     dstPtr = *((Ptr*) dstPtr);
  3168.     
  3169.     *((long*)( 0x04L + (long) dstPtr + vectorBit*8L)) = (long) handlerAddr;
  3170.     *((long*)( 0x08L + (long) dstPtr + vectorBit*8L)) = refCon;
  3171. }
  3172.  
  3173. void DMAIntRemove( unsigned long vectorBit)
  3174. {
  3175.     Ptr        ExpandPtr = (Ptr) 0x02B6L;
  3176.     Ptr        dstPtr;
  3177.     
  3178.     dstPtr = *((Ptr*)ExpandPtr);
  3179.     dstPtr += 0x0210;
  3180.     dstPtr = *((Ptr*) dstPtr);
  3181.     
  3182.     *((long*)( 0x04L + (long) dstPtr + vectorBit*8L)) = (long) 0x0060;
  3183.     *((long*)( 0x08L + (long) dstPtr + vectorBit*8L)) = 0L;
  3184. }
  3185.  
  3186.  
  3187. extern MADDriverRec *MODPlayMADDriver;
  3188.  
  3189. void    InstallMODVBL( MADDriverRec *MDriver)
  3190. {
  3191. //    THz             savedZone;
  3192.  
  3193.     MDriver->VBL.VBL.qLink        = 0L;        // VLBQueue
  3194.     MDriver->VBL.VBL.qType        = vType;
  3195.         
  3196. //    savedZone = GetZone();
  3197. //    SetZone( SystemZone());
  3198.     MDriver->VBL.VBL.vblAddr        = NewVBLProc( MODPlay);
  3199. //    SetZone( savedZone);
  3200.     
  3201.     MDriver->VBL.VBL.vblCount        = 0;
  3202.     MDriver->VBL.VBL.vblPhase        = 0;
  3203.     MDriver->VBL.VBLA5                    = (long) MDriver;
  3204.     
  3205.     MODPlayMADDriver = MDriver;
  3206.     
  3207.     InstallPersistentVBL( MDriver, &MDriver->VBL.VBL);
  3208. }
  3209.  
  3210. void    RemoveMODVBL( MADDriverRec *MDriver)
  3211. {
  3212.     OSErr    myErr;
  3213.     
  3214.     myErr = VRemove( (QElemPtr) &MDriver->VBL);
  3215.     
  3216.     if( MDriver->SysHeapPtr != 0L)
  3217.     {
  3218.         DisposeRoutineDescriptor( MDriver->VBL.VBL.vblAddr);
  3219.         
  3220.         DisposePtr( MDriver->SysHeapPtr);
  3221.         MDriver->SysHeapPtr = 0L;
  3222.     }
  3223. }
  3224.  
  3225. void InstallPersistentVBL( MADDriverRec *MDriver, VBLTask *theVBLRec)
  3226. {
  3227.     #define kJMPInstr     0x4EF9
  3228.     #define kJMPSize         6
  3229.     
  3230.     OSErr    myErr;
  3231.     Ptr        tempPtr;
  3232.  
  3233.     MDriver->SysHeapPtr = NewPtrSys( kJMPSize);
  3234.     if( MDriver->SysHeapPtr == 0L) DebugStr("\pNewPtrSys Error");
  3235.  
  3236.     *(short*) MDriver->SysHeapPtr = kJMPInstr;
  3237.     tempPtr = (Ptr) MDriver->SysHeapPtr + sizeof( short);
  3238.     
  3239.     *(ProcPtr*)tempPtr = (ProcPtr) theVBLRec->vblAddr;
  3240.     
  3241.     #if defined(powerc) || defined (__powerc)
  3242.     #else
  3243.     theVBLRec->vblAddr = (VBLUPP) ( MDriver->SysHeapPtr);
  3244.     #endif
  3245.     
  3246.     myErr = VInstall( (QElemPtr) theVBLRec);
  3247.     if( myErr) DebugStr("\pError in VInstall");
  3248. }
  3249. #endif*/